-2

I have an array like this:

Array(
    [0]=>Array([uploaderName]=>x[uploadedImageName]=>k6gIjfO[uploaderIp]=>195.155.116.217[uploader]=>e0699587cbfd[uploadedServer]=>alpha)
    [1]=>Array([uploaderName]=>x[uploadedImageName]=>byUTyJo[uploaderIp]=>195.155.116.217[uploader]=>e0699587cbfd[uploadedServer]=>alpha)
    [2]=>Array([uploaderName]=>x[uploadedImageName]=>oSVEnNk[uploaderIp]=>195.155.116.217[uploader]=>e0699587cbfd[uploadedServer]=>alpha)
    [3]=>Array([uploaderName]=>x[uploadedImageName]=>Dj7GRYS[uploaderIp]=>195.155.116.217[uploader]=>e0699587cbfd[uploadedServer]=>alpha)
    [4]=>Array([uploaderName]=>x[uploadedImageName]=>upsb8IC[uploaderIp]=>195.155.116.217[uploader]=>e0699587cbfd[uploadedServer]=>alpha)
    [5]=>Array([uploaderName]=>x[uploadedImageName]=>YoEEzGi[uploaderIp]=>195.155.116.217[uploader]=>e0699587cbfd[uploadedServer]=>alpha)
    [6]=>Array([uploaderName]=>x[uploadedImageName]=>st3dLNs[uploaderIp]=>195.155.116.217[uploader]=>e0699587cbfd[uploadedServer]=>alpha)
    [7]=>Array([uploaderName]=>x[uploadedImageName]=>LBNpiIG[uploaderIp]=>195.155.116.217[uploader]=>e0699587cbfd[uploadedServer]=>alpha)
    [8]=>Array([uploaderName]=>x[uploadedImageName]=>mFYDmBG[uploaderIp]=>195.155.116.217[uploader]=>e0699587cbfd[uploadedServer]=>alpha)
    [9]=>Array([uploaderName]=>x[uploadedImageName]=>z03kSx1[uploaderIp]=>195.155.116.217[uploader]=>e0699587cbfd[uploadedServer]=>alpha)
)

I want to get any image's data from this array.

Example: When user shows uploadedImageName == jCPjeWv, I wan't to get who is it's uploader.

3 Answers3

1

Simple, just use foreach

$arr = array(/* content here */);

foreach($arr as $value){
    if($value['uploadedImageName'] == 'jCPjeWv'){
        echo $value['uploaderName'];
        break;
    }
}
CarlosCarucce
  • 3,420
  • 1
  • 28
  • 51
1

Just for fun, here's another way:

echo array_column($array, 'uploaderName', 'uploadedImageName')['jCPjeWv'];
  • Get an array of uploaderName with the index set to uploadedImageName
  • Access the index using the uploadedImageName 'jCPjeWv'

Obviously to do it multiple times you would want to actually create a new array:

$images = array_column($array, 'uploaderName', 'uploadedImageName');
echo $images['jCPjeWv'];

If you want to access the other values as well, then use null instead of uploaderName:

$images = array_column($array, null, 'uploadedImageName');
echo $images['jCPjeWv']['uploaderName'];
echo $images['jCPjeWv']['uploaderIp'];

NOTE: These ways only work if the uploadedImageName is unique.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • Good trick. Doesn't it take too much resources from the server (Memory/Processing)? – CarlosCarucce Jun 13 '16 at 19:08
  • @CarlosCarucce: Doubt it, but added a reusable example. – AbraCadaver Jun 13 '16 at 19:10
  • To clarify for researchers, using a `break` -able `foreach()` will likely perform better than functional-style approaches (`array_column()` and `array_filter()`) because `foreach()` has the potential to stop iterating as soon as it finds a match. Conversely, functional iterators cannot stop when a match is found -- they will unconditionally iterarate the entire array. – mickmackusa Nov 17 '22 at 03:39
0

A foreach is possible, but I think it's important to learn the array functions as well so I'm just going to put this example here.

$uploadedImageName = 'jCPjeWv';
$filtered = array_filter($array, function($value) use ($uploadedImageName) {
   return ($value['uploadedImageName'] == $uploadedImageName);
});

This will return a $filtered with the other arrays removed since their $value['uploadedImageName'] will not equal $uploadedImageName.

For more info check out the http://php.net/manual/en/function.array-filter.php manual.

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
  • 1
    To clarify for researchers, using a `break` -able `foreach()` will likely perform better than functional-style approaches (`array_column()` and `array_filter()`) because `foreach()` has the potential to stop iterating as soon as it finds a match. Conversely, functional iterators cannot stop when a match is found -- they will unconditionally iterarate the entire array. – mickmackusa Nov 17 '22 at 03:39
  • Yes that is true, most languages have a concept of a "first" or "single" match filter, but it's been a while since I've used PHP, I can't recall if that type of function exists natively. – Devon Bessemer Nov 17 '22 at 14:25