mahmoud mohamed mahmoud mohamed - 3 months ago 9
PHP Question

check if value exist in each array multidimensional array

i hava multidimensional array

<?php

$data = array(
array(
'name'=>'ahmed',
'job'=>'engineer',
'age'=>25,
'hobbies' => array('drawing','swimming','reading'),
'skills' => array('coding','fasting learning','teaching')
),
array(
'name'=>'Sara',
'job'=>'designer',
'age'=>19,
'skills'=>array('fast learning')
) ,
array(
'name'=>'Ali',
'age'=>25,
'city'=>'cairo'
),

array(
'name'=>'Hossam',
'job'=>'accountant',
'age'=>25,
'city'=>'zagazig'
),
array(
'name'=>'Esraa',
'job'=>'Designer',
'age'=>23,
'city'=>'zagazig',
'hobbies' => array('writing','reading'),
'skills' => array('coding','teaching')
),
);


i want count Arrays where city = "zagazig" or "cairo"

and echo Array Values


Example :


  • There is [ 1 ] people of City => [ cairo ] :



---------------- Result -----------------------


  • Name => Ali

  • Age => 25

  • City => cairo







if City
!exist
echo Values


Example :

*---------------- Invaild Data -------------

----------------------- First ---------------


  • Name => Sara

  • Job => designer

  • Age => 19

  • Skill => fast learning



----------------- Second ----------------


  • Name => ahmed

  • Job => engineer

  • Age => 25



-------------------- Hobbies ----------------


  • drawing

  • swimming

  • reading



-------------------- Skills ----------------


  • coding

  • fasting learning

  • teaching







but i don't know how to loop Multidimensional Array

Answer

Given that this is just a raw array, a simple if with foreach should suffice.

First, if the criteria is to get certain entries using city, just use a stripos to search;

$search_string = 'zagazig';
$results = array();
foreach($data as $value) {
    if(
        !empty($value['city']) &&
        (stripos($value['city'], $search_string) !== false)
    ) {
        $results[] = $value;
    }
}

This checks if the entry has a city index, then just pushes that array inside a container $result. After gathering the results, just loop it like any normal array:

if(!empty($results)) {
    echo 'Number of results: ' , count($results), '<br/> Result <hr/>';
    foreach($results as $r) {
        echo "
        Name: {$r['name']}
        Job: {$r['job']}
        Age: {$r['age']} <br/>
        ";
        echo !empty($r['hobbies']) ? '<br/>Hobbies: <br/>' . implode('<br/>', $r['hobbies']) : '';
    }
}

Output

Of course you can use a <table> tag if you want, this is just an ugly example.

If you like something a little bit different, you can also use array_filter:

Here's an example of it (this also includes some searching inside hobbies and skills):

$search_string = 'coding';
$criteria = 'skills';
$results = array_filter($data, function($e) use ($search_string, $criteria) { 
    return (
        !empty($e[$criteria]) && 
        (!is_array($e[$criteria]) 
            ? (strpos($e[$criteria], $search_string) !== false)
            : (in_array($search_string, $e[$criteria]))
        )
    );
});

Output

Comments