How to get element from array? - php

How to get single element from this?
array(1) { [0]=> object(stdClass)#3 (2) { ["id"]=> int(29595) ["image_id"]=> string(20) "eohsidatfx8wyw5ltzt6" } }
I need to separate "image_id". How to do it? I tried
echo $result["image_id"]
but it doesn't work:
Notice: Undefined index: image_id in C:\xampp\htdocs\IGDB\moje\index.php on line 53

It seems your array only directly contains object(stdClass)#3. This object is itself an array containing id and image_id. You can access image_id by doing
echo $result[0]["image_id"];

Ok, got it.
$result3=array_column($result2, 'image_id');
echo $result3[0];

$myArray = array(
'#3' => array (
"id"=> 29595,
"image_id"=> "eohsidatfx8wyw5ltzt6"
)
);
what you are looking for is in the second level of your array.
Use a foreach loop to iterate of the arrays key/value pairs.
foreach($myArray as $value){
foreach($value as $key => $id){
if($key === 'image_id'){
$output = $id;// output now holds the vlaue of the key set with 'image_id'
}
}
}
If you know the value of the key, you can also access this by using the keys like so: $arrayname['firstlevelkey']['secondlevelkey'];
Notice: Undefined index: image_id in C:\xampp\htdocs\IGDB\moje\index.php on line 53
--> This is because you are defining an array with a key that does not exist in the array
echo $result["image_id"] --> here you are telling php that "image_id" is on the first level of the array, however, it looks to be nested in the second layer of the array you are trying to parse. $result['#3']['image_id'].
If you are not sure, write a conditional that looks in the first array using is_array(), if the first is a key value holding a child array. Then run the foreach loop again to look for the key/pair value.
foreach($arr as $values){
// do something if value is string
if(is_array($values){
foreach($values as $key => $value){
// check your second level $key/$value
}
}
}

Related

Get value out of Array

I am trying to get a certain value out of an array.
Example of the array is:
array(2) {
["error"]=>
array(0) {
}
["result"]=>
array(1) {
["open"]=>
array(1) {
["12345-AAAAA-66AAKK"]=>
array(14) {
["inf"]=>
Usually when I want a certain value I would use:
$datawanted=$data[result][open][value];
However, in this case the first array is a variable that always changes (12345-AAAAA-66AAKK), I need to find the value of that.
I tried getting this with reset() and key[0] but this not give the wanted result.
Is there a way to get the output of the first element in the result array?
You can use array_search: http://php.net/manual/de/function.array-search.php
Example:
foreach ($array['result']['open'] as $dynamicKey => $item) {
if ($key = array_search('Value you are looking for', $item) {
$datawanted=$array['result']['open'][$dynamicKey][$key];
}
}
$data[result][open] is not a correct way to access array items.
The token result looks like a constant. PHP searches for a constant named result, cannot find one and triggers a notice. Then it thinks "I guess the programmer wanted to write 'result' (a string, not a constant). I'll convert it as string to them." and uses 'result' instead.
It works but it's a horrible practice. It dates from the prehistory of PHP, 20 years ago and it's not recommended.
After you fix your code to correctly denote the keys of an array, the next step is to pick one of the many PHP ways to access values in the array.
You can get the first value of an array without knowing its key ($data['result']['open']['12345-AAAAA-66AAKK']) by using the function reset():
$datawanted = reset($data['result']['open']);
Or you can use the function array_values() to get only the values of the array (the keys are ignored, the returned array have the values indexed from zero) then your desired data is at position 0 on this array:
$values = array_values($data['result']['open']);
$datawanted = $values[0];
Another option, if you don't need to keep $data for further processing, is to use the PHP function array_shift() to remove the first value from the array and return it. Be warned that this function modifies the array it receives as argument:
$datawanted = array_shift($data['result']['open']);
If you need to process all the values of $data['result']['open'] (and you probably do) then the best way is to use the foreach PHP statement. It allows you to access both the key and the value of each element of the array:
foreach ($data['result']['open'] as $key => $value) {
// $key is '12345-AAAAA-66AAKK'
$datawanted = $value;
}

Trying to create an associative array PHP

I have the following array. What I'm trying to do is get each element under "bill_ids", use the ID (e.g. "hjres61-114") to make another call and then retitle the 0 under "bill_ids" to the ID and then include another array under that element.
Here's what I have and it's giving me this error..
Message: Illegal offset type
$floor_updates = $this->congress->floor_updates($params);
foreach ($floor_updates as $update) {
if ($update['bill_ids']) {
foreach ($update['bill_ids'] as $bill => $bill_id) {
$billInfo = $this->bill->billSearch(['bill_id' => $bill_id]);
$floor_updates[$update]['bill_ids'][$bill][0] = $billInfo;
}
}
}
I'm terrible with php arrays and any guidance would be greatly appreciated..
What you actually want to do is the following:
First, capture the array index of each of our update elements. We can simply do that by passing in $array_index => $update.
foreach ($floor_updates as $array_index => $update)
Now, we can access the $update array by $floor_updates[$array_index].
$floor_updates[$array_index]['bill_ids'][$bill] = $billInfo;
In the above, there's no reason to access the 0th element of the array as the $bill actaully contains reference to the index of each key value pair, so we can simply just reference [$bill] to get access to the array.

How to get value by value of an array

I got a problem, I got a array from a sql query and I want to associate each value of this array to an index value.
array(16) { ["noCommande"]=> string(5) "49083" ["dateCommande"]=> string(19) "2007-02-21 18:24:04" ...
So here I just want to get back each value one per one. The array[i] doesn't work so I am a bit in trouble.
Thanks you for your support.
It's associative array, values are in
$array['noCommande'];
$array['dateCommande'];
etc.
If you want to ake a loop over array and write all values,
foreach ($array as $key => $value) {
echo $key . ': ' . $value; // echoes 'noCommande: 49083', etc.
}
You can iterate like below:-
foreach($your_array as $key=>$value){
echo $key.'-'.$value;
echo "<br/>";//for new line to show in a good manner
}
It will output like :- noCommande - 49083 and etc.

PHP Find Array Index Name

Let's say I have an array that looks like this when I perform a var_dump:
array(1) { [300000001]=> string(15) "Find Compatible" }
Instead of printing the value "Find Compatible", how can I use the index name 300000001 as a variable?
first key:
$first_key = key($array);
search based on value:
$key=array_search('Find Compatible',$array);
If you want to get all the keys of a given $array, you can iterate using foreach:
foreach ($array as $key => $value) {
echo $key;
}

Get key from an array in smarty

If I have an array that looks like this:
$my_array = array(2) { ["mykey"]=> int(2) ["mysecondkey"]=> int(3) }
How can I get to the key value of the first element?.
So far I know I can do $my_array[0], but how do I get to the first key? I want to avoid doing foreach.
Thanks!
This is associative array (string keys). Use name of key directly:
$my_array["mykey"];
Read more here
To extract keys use array_keys:
$my_array = array(2) { ["mykey"]=> int(2) ["mysecondkey"]=> int(3) }
$keys = array_keys($my_array); //0 => "mykey", 1 => "mysecondkey"
You can use array_keys() to get a list of your array's keys:
$keys = array_keys($my_array);
echo $keys[0]; // outputs "mykey"
If you want to do this in Smarty, you can use the following code to output the array's first key:
{foreach from=$my_array key=my_key item=i name=my_array}
{if $smarty.foreach.my_array.first}{$my_key}{/if}
{/foreach}
foreach($my_array as $key=>$_val){
echo $key;
break;
}
array_keys() will get all the keys in an array, while this will get the first key without iterating all the others and ignoring the rest of the array.
It is a fake loop as the break will exit at the first iteration, so you are not actually doing a real foreach for all the values.
foreach($my_array as $firstKey=>$unused) {
break;
}
echo($firstKey);
If you need a function, you may use this:
function first_key($arr) {
foreach($my_array as $firstKey=>$unused) {
return $firstKey;
}
}
Again the function returns at the first iteration, so you are not doing a "real" foreach on all the elements.

Categories