Get values from custom array using php - php

I have got the below array
Array
(
[0] => Array
(
[0] => Contact Number
[1] => 35443545
)
[1] => Array
(
[0] => Address
[1] => vvvv
)
)
I would like to display as
Contact Number
35443545
<hr>
Address
vvvv
My code
foreach($address_box_content as $k=>$address)
{
echo '<h2>'.$address[$k].'</h2><p>'.$address[$k+1].'</p>';
}
But for some reason it is printing the first 2 lines and then displaying a notice 'Undefined offset:2'

What you have is an array. One whose elements are (also) arrays. Each array that you have stored has 2 elements, 0 and 1.
When you loop, $k is the index of the main (outer) array. Its value doesn't make any sense in the inner array. You just need to loop over the outer array, and print the 0 and 1 elements from the inner one.
foreach($address_box_content as $address)
{
echo '<h2>'.$address[0].'</h2><p>'.$address[1].'</p>';
}

Well you're getting undefined offset error because you're using $k which is the index of the outer array
You can do something like:
foreach($address_box_content as $addresses){
foreach($addresses as $address){
echo '<h2>', $address, '</h2><p>', $address, '</p>';
}
}
And if you want to get the index of inner array:
foreach($address_box_content as $addresses){
foreach($addresses as $key => $address){
echo '<h2>', $address[$key], '</h2><p>', $address[$key], '</p>';
}
}

This is your array
$data = Array
(
[0] => Array
(
[0] => Contact Number
[1] => 35443545
)
[1] => Array
(
[0] => Address
[1] => vvvv
)
)
simple way to print array
for($i=0;$<count($data);$i++)
{
echo "<h2>".$data[$i][$0]."</h2>"."<p>".$value[$i][1]."</p>"."<hr />";
}

well, for each element you define a key and a value which are $k and $address.
$k will be 0 and 1, $address will be 0 and 1, and then 0 and 1.
the undefined offset error is because you call $k plus 1 that on the second foeach iteration try to access to position 1+1 (2) doesn't find anything.
you could use something like:
foreach($address_box_content as $data => $value) {
echo "<h2>" . $value[0] . "</h2>"
. "<p>" . $value[1] . "</p>"
. "<hr />";
}

Related

PHP - What is the correct way to handle this strange array?

I'm trying to break up this array with a foreach loop, and kind of have it working, but can't quite get what I need.
My array is as follows:
Array
(
[0] => Array
(
[qty] => 5
[itemId] => 247
)
[1] => Array
(
[qty] => 3
[itemId] => 1
)
)
What I want, is to loop through the above array and be able to print the following:
Quantity: 5
Item: 247
Quantity: 3
Item: 1
The problem I have is, I'm able to loop through, I just can't distinguish between the qty and itemId. It's because it's multi-dimensional and I'm clearly handling it wrong.
Here is what I have so far $idQty is my array and contains the info above:
foreach($idQty as $orders => $order) {
foreach($order as $k) {
print_r("Quantity"+$k)
}
}
and what I get is everything as a Quantity. I don't know how to distinguish between the 2. Any help ?
Thanks,
Iterating through the orders you'll be able to access each of the sub-arrays. You want to do it this way:
foreach($idQty as $index => $order) {
echo "Quantity: " . $order['qty'];
echo "Item: " . $order['itemId'];
}
u need only 1 loop
$idQty = array(['qty' => 5, 'itemId' => 247], ['qty' => 3, 'itemId' => 1]);
foreach ($idQty as $value) {
print_r('Quantity:' . $value['qty'] . PHP_EOL);
print_r('Item:' . $value['itemId'] . PHP_EOL);
}

How do I get value of specific array element by secondary key?

This has to be easy but I am struggling with it. If the array below exists (named "$startersnames") and I specifically want to echo the value that has "qb" as the key, how do I do that?
I assumed $startersnames['qb'], but no luck.
$startersnames[0]['qb'] works, but I won't know that it's index 0.
Array
(
[0] => Array
(
[qb] => Tannehill
)
[1] => Array
(
[rb] => Ingram
)
[2] => Array
(
[wr] => Evans
)
[3] => Array
(
[wr] => Hopkins
)
[4] => Array
(
[wr] => Watkins
)
[5] => Array
(
[te] => Graham
)
[6] => Array
(
[pk] => Hauschka
)
[7] => Array
(
[def] => Rams
)
[8] => Array
(
[flex] => Smith
)
)
You can use array_column (from php 5.5) like this:
$qb = array_column($startersnames, 'qb');
echo $qb[0];
Demo: http://3v4l.org/QqRuK
This approach is particularly useful when you need to print all the wr names, which are more than one. You can simply iterate like this:
foreach(array_column($startersnames, 'wr') as $wr) {
echo $wr, "\n";
}
You seem to be expecting an array with text keys and value for each, but the array you have shown is an array of arrays: i.e. each numeric key has a value which is an array - the key/value pair where you are looking for the key 'qb'.
If you want to find a value at $array['qb'] then your array would look more like:
$array = [
'qb' => 'Tannehill',
'rb' => 'etc'
];
now $array['qb'] has a value.
If the array you are inspecting is a list of key/value pairs, then you have to iterate over the array members and examine each (i.e. the foreach loop shown in your first answer).
For your multi-dim array, you can loop through the outer array and test the inner array for your key.
function findKey(&$arr, $key) {
foreach($arr as $innerArr){
if(isset($innerArr[$key])) {
return $innerArr[$key];
}
}
return ""; // Not found
}
echo findKey($startersnames, "qb");
You can try foreach loop
$key = "qb";
foreach($startersnames as $innerArr){
if(isset($innerArr[$key])) {
echo $innerArr[$key];
}
}
$keyNeeded = 'qb';
$indexNeeded = null;
$valueNeeded = null;
foreach($startersnames as $index => $innerArr){
//Compare key
if(key($innerArray) === $keyNeeded){
//Get value
$valueNeeded = $innerArr[key($innerArray)];
//Store index found
$indexNeeded = $index;
//Ok, I'm found, let's go!
break;
}
}
if(!empty($indexNeeded) && !empty($valueNeeded)){
echo 'This is your value: ';
echo $startersnames[$indexNeeded]['qb'];
echo 'Or: ':
echo $valueNeeded;
}
http://php.net/manual/en/function.key.php

PHP Array - obtaining vlaues

I have an array, the output of print_r is:
Array ( [0] => Array ( [ID] => 1 [col1] => 1 ) [1] => Array ( [ID] => 2 [col1] => 2 ) )
Could you help of kind of array it is? So that I could research more about it? What I want is to get ID and col1 values
I've tried to use foreach:
foreach($array_name as $key=>$value){
print "$key holds $value\n";
}
The output I get is 0 holds Array 1 holds Array
And I would simply like to get:
1 1
2 2
It's a multi dimensional array, or an array where each element is another array. So you'll need to loop twice. Try this to look at it:
foreach($array_names as $arr)
{
foreach($arr as $key => $val)
{
print "$key = $val\n";
}
}
Or, to get your just added desired output, do this:
foreach($array_names as $arr)
{
foreach($arr as $key => $val)
{
print "$val ";
}
print "\n";
}
Or this:
foreach($array_names as $arr)
{
print $arr['ID'] . " " . $arr['col1'] . "\n";
}
or a few other ways but you should be getting the picture.

unable to fetch data from Mongo dump

Below is the dump that I got from mongo. I need to fetch the opening artist name.
Array
(
[_id] => MongoId Object
(
[$id] => 51c9b63b6f7cb5f8229f27b7
)
[s20] => Array
(
[opening] => Array
(
[artist] => Array
(
[name] => Jay Z
)
[music] => Array
(
[name] => 99 problems
)
)
)
So, I tried:
foreach($mongo_dump as $key=>$value){
echo "<pre>KEY: " . print_r($key["s20"]["opening"]["artist"]["name"]) . "</pre>"; // line # 16
echo "<pre>VALUE: " . print_r($value) . "</pre>";
echo "\n\n";
}
However, I did not get the artist name. I received the following PHP warning:
PHP Warning: Illegal string offset 's20' in /var/www/Code/analytics/fetch_top_5_opening_artists.php on line 16
As Blaine mentions, $key isn't an array. The way that you are traversing the dump is incorrect. $key becomes a string in the context of the foreach loop. Try doing something like this:
if ($key == "s20") {
echo "<pre>KEY: " . print_r($value["opening"]["artist"]["name"]) . "</pre>";
}
the value itself is array() so your forloop is not going work unless you setupup nested. Here is example of neted for loop.
foreach($mongo_dump as $key )
{
{
foreach($key as $subkey)
{
echo $subkey
echo "\n\n";
}
}

Question related to array

I'm trying to print array. All code working fine.But at last I'm getting `ArrayArray'. Can any one solve this problem. many many thanks
here is my array
Array
(
[Post1] => Array
(
[id] => 1
[title] => hi
)
[Post2] => Array
(
[0] => Array
(
[id] => 1
)
)
[Post3] => Array
(
[0] => Array
(
[id] => 1
)
)
)
Here is my PHP Code
foreach($post as $key => $value) {
foreach($value as $print => $key) {
echo "<br>".$key;
}
}
here is output
ID
Array
Array
Try this:
foreach($post as $key => $value) {
foreach($value as $print => $key) {
if (is_array($key)){
foreach($key as $print2 => $key2) {
echo "<br>".$key2;
}
}else{
echo "<br>".$key;
}
}
}
The to string method of an array is to return "Array".
It sounds like you want to view the array for debugging purposes. var_dump() is your friend :)
you are trying to print an array, resulting in Array.
If you want to print an array use print_r
I think the trouble for you is that you have $key in the outer loop and $key in the inner loop so its really confusing which $key you are talking about for starters.
You just want the stuff printed out to debug?
echo "<pre>" . print_r( $post , true ) . "</pre>\n";

Categories