How to get index of multidimensional array in php

I have an array like that:

printr_[$photos];
======

Array
[
    [0] => Array
        [
            [path] => site:photos/photo-1.jpg
            [data] => Array
                [
                    [PhotoTitle] => Mega title
                    [PhotoDate] => 2015
                    [FlickrURL] => xxx
                    [Portrait] => 
                    [slug] => mega-title
                ]

        ]

    [1] => Array
        [
            [path] => site:photos/photo-2.jpg
            [data] => Array
                [
                    [PhotoTitle] => Photo title
                    [PhotoDate] => 2001
                    [FlickrURL] => xxx
                    [Portrait] => 
                    [slug] => photo-title
                ]

        ]

...

I would like to get the array index from a string [which is the slug from my browser current URL].
Tried that solution but I got an error [Undefined index: data […] on line 95]

/*
** Search in array
*/
function arraySearch[$array, $field, $search]{
    foreach[$array as $key => $value]{
        if [$value[$field] === $search]
            return $key;
    }
    return false;
}

// Line 95
$photo_index = $photos[arraySearch[$photos["data"], "slug", "mega-title"]];

asked Apr 9, 2015 at 20:16

6

This should work PHP >= 5.5:

$key = array_search['mega-title', array_column[array_column[$photos, 'data'], $slug]];
$photo_index = $photos[$key];

answered Apr 9, 2015 at 20:25

AbraCadaverAbraCadaver

77.5k7 gold badges62 silver badges84 bronze badges

3

The $photos variable does not have a key 'data', at least not by the print output. It has keys 1, 2, 3 etc.So $photos["data"] simply does not exist.

As to the search, I'm not 100% sure what you want to do, but I assume you want to get the index of the photo which $field in data equals $search, so that would be

/*
** Search in array
*/

function arraySearch[$array, $field, $search]{
    foreach[$array as $key => $value]{
        if [$value["data"][$field] === $search]
            return $key;
    }
    return false;
}

and you would use it

$photo_index = $photos[arraySearch[$photos, "slug", "mega-title"]];

answered Apr 9, 2015 at 20:25

Jiří KantorJiří Kantor

6941 gold badge5 silver badges12 bronze badges

array_search[] is a built in PHP function that searches the array for a given value and returns the first corresponding key if successful.

Syntax

array_search [ mixed $needle , array $haystack [, bool $strict = FALSE ] ] : mixed

Support: [PHP 4 >= 4.0.5, PHP 5, PHP 7]

Parameters of array_search[]

$needle

  • The $needle parameter is required.
  • It refers to the value that needs to be searched in the array.

$haystack

  • The $haystack parameter is required.
  • It refers to the original array, on which search operation is performed.

$strict

  • The $haystack parameter is an optional one.
  • It can be set to true or false, and refers to the strictness of search.
  • The default value of this parameter is false.
  • Example: when it is set to true, the string 45 is not same as number 45.

Return Values

  • The key of a value is returned if it is found in an array, otherwise false.
  • If the value is found in the array more than once, the first matching key is returned.

Example #1 – Search an array for the value “Jerusalem” and return its key

$records = [
  'New Delhi' => 'India',
  'Madrid' => 'Spain',
  'Israel' => 'Jerusalem',
  'Amsterdam' => 'Netherlands'
];
$countries = array_search["Jerusalem", $records];
print_r[$countries];
The above example will output:
Israel

Example #2 – With third parameter “strict” mode

$records = [
  'x' => '10',
  'y' => 10,
  'z' => '10'
];
echo 'Without strict parameter: ';
echo array_search[10, $records];
echo 'With strict parameter: ';
echo array_search[10, $records, true];
The above example will output
Israel

Example #3 – You don’t have to write your own function to search through a multidimensional array, [example with array_column[]]

$records = [
    [
        'id' => 12,
        'country' => 'India',
        'capital' => 'New Delhi',
    ],
    'Spain' => [
        'id' => 24,
        'country' => 'Spain',
        'capital' => 'Madrid',
    ],
    [
        'id' => 91,
        'country' => 'Israel',
        'capital' => 'Jerusalem',
    ],
    'Netherlands' => [
        'id' => 121,
        'country' => 'Netherlands',
        'capital' => 'Amsterdam',
    ]
];
$countries = array_column[$records, 'country'];
print_r[$countries];
$key = array_search['Israel', array_column[$records, 'country']];
echo 'Key: ' . $key;
The above example will output
Array
[
    [0] => India
    [1] => Spain
    [2] => Israel
    [3] => Netherlands
]
Key: 2

Hope you find this helpful

How get key of multidimensional array in PHP?

Retrieving Values: We can retrieve the value of multidimensional array using the following method:.
Using key: We can use key of the associative array to directly retrieve the data value. ... .
Using foreach loop: We can use foreach loop to retrieve value of each key associated inside the multidimensional associative array..

What is multidimensional array in PHP explain it with simple PHP code?

A multidimensional array is an array containing one or more arrays. PHP supports multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people.

How would you access the word it from the multidimensional array?

To access an element of the multidimensional array, you first use square brackets to access an element of the outer array that returns an inner array; and then use another square bracket to access the element of the inner array.

How can create multidimensional array in array in PHP?

You create a multidimensional array using the array[] construct, much like creating a regular array. The difference is that each element in the array you create is itself an array. For example: $myArray = array[ array[ value1 , value2 , value3 ], array[ value4 , value5 , value6 ], array[ value7 , value8 , value9 ] ];

Chủ Đề