php multidimensional array problems - php

I want to output only 1 name data from my array which is:
Array
(
[results] => Array
(
[0] => Array
(
[0] => some name
[1] => Founder
)
[1] => Array
(
[0] => some name
[1] => Marshal
)
[2] => Array
(
[0] => some name
[1] => Marshal
)
[3] => Array
(
[0] => some name
[1] => Royal Knight
)
[4] => Array
(
[0] => some name
[1] => Knight
)
)
)
1
now I use :
echo "<pre>";
echo print_r($API->getSearchFreeCompanyMembers());
echo "</pre>";
to print the array which seems to work fine but when I try selecting single array like:
echo "<pre>";
echo print_r($API->getSearchFreeCompanyMembers(1)[0]);
echo "</pre>";
all I get is 1 on the page
Any help would be much appreciated, if any more code needed please let me know.

You seem to have changed the arguments when calling the method. getSearchFreeCompanyMembers
Your first example shows getSearchFreeCompanyMembers()
The second is getSearchFreeCompanyMembers(1)
To get the first element in the array returned by the method.
1. Dereference as you did (just do not put 1 as argument).
$result = $API->getSearchFreeCompanyMembers()[0]
Beware the side effect is that the rest of the array returned is discarded. Also this feature is only available from <= 5.4
2. Save the returned array to a variable and pick the first element
$array = $API->getSearchFreeCompanyMembers();
print_r($array[0]);
For more info on arrays and for dereference see example #7
http://php.net/manual/en/language.types.array.php

You may try something like this
$array = $API->getSearchFreeCompanyMembers();
echo $array['results'][0][1]; // first name (Founder)
echo $array['results'][1][1]; // second name (Marshal)
Or, use (for some name)
echo $array['results'][0][0]; // first ('some name')
echo $array['results'][1][0]; // second ('some name')
Here, results is an associative array and the first array is [0] and every array ([0], [1]) in results contains an array. So, it's something like this
$array = Array(
'results' => Array (
Array ( 'some name', 'Founder' ), // 1st array in results
Array ( 'some name', 'Marshal' ) // 2nd array in results
)
);

echo "<pre>";
$members = $API->getSearchFreeCompanyMembers();
print_r($members['results'][0]); //no echo needed
echo "</pre>";

like to print array results key [0] value [1] i.e founder. you have to call array like this
before print_r there is no need of echo
echo "<pre>";
print_r($results [0][0]);
echo "</pre>";
similarly for
echo "<pre>";
$employee=$API->getSearchFreeCompanyMembers();
print($employee['results'][1][0]);
echo "</pre>";

Related

Array values to single array using foreach loop in PHP

I am working with php and arrays, I have multiple arrays like following
Array
(
[0] => Array
(
[wallet_address] => 0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx
)
[1] => Array
(
[wallet_address] => 0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
[2] => Array
(
[wallet_address] => 0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
and so on....
And i want to make them in single array with comma like following way
$set = array("0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx","0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx","0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
How can i do this ?Here is my current code but not working,showing me same result(0,1,2 keys),Where i am wrong ?
$GetUserFollower; //contaning multiple array value
$set=array();
foreach($GetUserFollower as $arr)
{
$set[]=$arr;
}
echo "<pre>";print_R($set);
The original array is an Assoc array and therefore the wallet_address needs to be addressed specifically in a loop. Or you could use the array_column() builtin function to achieve the same thing.
$GetUserFollower; //contaning multiple array value
$set=array();
foreach($GetUserFollower as $arr)
{
$set[] = $arr['wallet_address'];
}
echo "<pre>";print_r($set);
Or
$new = array_column($GetUserFollower, 'wallet_address');
print_r($new);
RESULT
Array
(
[0] => 0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx
[1] => 0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx
[2] => 0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
Your comments are making me think you want an array without a key, which is impossible. If you do this with the example you show in your comments
$set = array("0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx","0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx","0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
print_r($set);
You will see
Array
(
[0] => 0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx
[1] => 0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx
[2] => 0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)

php count not working properly in my array

i print php array when it's empty it print 1
$addresses = $user->myFunction();
print_r(count($addresses['my_test']));
Array
(
[my_test] => Array
(
[test] => test
[test1] => test1
)
)
it print 2
when i print this
Array
(
[my_test] => Array
(
[] =>
)
)
i got 1
i don't know what is the problem here
How to print exact value of this?
Array count all element including empty ones. Your output is correct as you second array has 1 element.
Consider use array_filter to avoid them.
Example:
$a = array("" => "");
echo count($a). PHP_EOL; // echo 1
$a = array_filter($a);
echo count($a). PHP_EOL; // echo 0
In you case:
print_r(count(array_filter($addresses['my_test'])));
Array
(
[my_test] => Array
(
[test] => test
[test1] => test1
)
)
print_r(count($addresses['my_test'])); // it will show 2 cause you have 2 values inside my_test key.
print_r(count($addresses)); // it will show 1 cause you have one value inside main array
Array
(
[my_test] => Array
(
[] =>
)
)
print_r(count($addresses['my_test'])); // it will show 0 because you have 0 values inside my_test key.
print_r(count($addresses)); //it will show 1 because you have one value my_test inside your main array.
Hope it helps you clarify count function.

PHP add/remove values from array created by json

I have an array that looks like this after using json_decode() on a response from a Json webservice:
[11] => Array
(
[0] => 80B37803-6278-5351-BC7A-D3A2FBFF8AA7
[1] => test
[2] =>
)
[12] => Array
(
[0] => 70B37803-6278-5351-BC7A-D3A2FBFF8AA8
[1] => test 2
[2] =>
)
[13] => Array
(
[0] => 90B37803-6278-5351-BC7A-D3A2FBFF8AA9
[1] => test 3
[2] =>
)
To print the array I use the following code:
echo '<pre>'; print_r($responseArticle); echo '</pre>';
How can I edit this kind of array in order, for example, to add a 3rd row or delete one of the already exsisting row?
$newArray = json_decode($json_data);
add inner data
adding a column for 11 number of row
$newArray[11][4] = 'this is the 4th column';
for delete
unset($newArray[11][4]);
for adding row
$newArray[lastindex] = array('90B37803-6278-5351-BC7A-D3A2FBFF8AA9','test4','');
for delete row
unset($newArray[index]);
to add another value of an array is to use array_push and to delete it you can use unset. See the example below.
<?php
$json_array[11] = array('80B37803-6278-5351-BC7A-D3A2FBFF8AA7','test','');
$json_array[12] = array('70B37803-6278-5351-BC7A-D3A2FBFF8AA8','test 2','');
$json_array[13] = array('90B37803-6278-5351-BC7A-D3A2FBFF8AA9','test 3','');
array_push($json_array, array('90B37803-6278-5351-BC7A-D3A2FBFF8AA9','test 4',''));
echo '<pre>';print_r($json_array);echo '</pre>';
unset($json_array[14]);
echo '<pre>';print_r($json_array);echo '</pre>';
?>
To add in some info for N key:
$responseArticle[N] = array('80B37803-6278-5351-BC7A-D3A2FBFF8AA7', 'testN', '');
To delete info stored in N key:
if(isset($responseArticle[N]) {
unset($responseArticle[N]);
}
Here N can be any number, say 3 as you have asked in the question.

Print array php

I got this array output in php:
Array
(
[blabla0] => Array
(
[0] => lalala
[1] => lelele
)
[blabla1] => Array
(
[0] => lalala
[1] => lelele
)
)
If I'd like to print by associative name, it's like this: $myArray['blabla0'][0] , and it works. But I want to do like this: echo $myArray[0][0], by index...is there any way?
Mark bakers answer is best for one option. You can convert all of the array with array_values eg
$new_array = array_values($old_array);
echo $new_array[0][0];
It depends how many times you wish to access the array
http://php.net/manual/en/function.array-values.php

Selecting value from array results

Being new to learning PHP I am having trouble with understanding how to select/echo/extract a value from array result a API script returns.
Using the standard:
echo "<pre>";
print_r($ups_rates->rates);
echo "</pre>";
The results returned look like this:
Array
(
[0] => Array
(
[code] => 03
[cost] => 19.58
[desc] => UPS Ground
)
[1] => Array
(
[code] => 12
[cost] => 41.69
[desc] => UPS 3 Day Select
)
[2] => Array
(
[code] => 02
[cost] => 59.90
[desc] => UPS 2nd Day Air
)
)
If I am only needing to work with the values of the first array result: Code 3, 19.58, UPS Ground --- what is the correct way to echo one or more of those values?
I thought:
$test = $ups_rates[0][cost];
echo $test;
This is obviously wrong and my lack of understanding the array results isn't improving, can someone please show me how I would echo an individual value of the returned array and/or assign it to a variable to echo the normal way?
echo $ups_rates->rates[0]["cost"];
See Arrays
More:
To iterate over the array
foreach ($ups_rates->rates as $rate) {
echo $rate["cost"];
// ...
}
$array = $ups_rates->rates;
$cost = $array[0]['cost'];
echo $ups_rates->rates[0]['code'];
echo $ups_rates->rates[0]['cost'];
echo $ups_rates->rates[0]['desc'];
should print out all 3
rates[0] is the first element of your array and you can index into that array by appending a ['key'] index onto the end
the only thing you forgot is ' marks

Categories