Getting a value from a php array using the index number - php

I have an array called $alldata
If I do this
echo $alldata[0][6][0]["COLOUR"];
It successfully returns the colour. I want to access the value without using the name / label "COLOUR"
I tried this, but it fails with undefined offset
echo $alldata[0][6][0][0];

Re-index so you can use a numeric index:
echo array_values($alldata[0][6][0])[0];
Or for them all:
$result = array_values($alldata[0][6][0]);
echo $result[0];
echo $result[2];

You have to use foreach for this, since the array key is "COLOUR" and not 0.
here's an example on how to solve your problem.
<?php
$alldata = array(
0=>array(
6=>array(
0=>array(
"COLOR"=>"test"
))));
print_r($alldata);
foreach ($alldata[0][6][0] as $key => $value) {
echo $key . "=>" . $value;
}
?>
if you want to use the third key, then you can add a counter to it, by defining $x outside of the foreach and $x++; in the foreach.

Related

PHP dynamic array name with loop

I need to get values of array through a loop with dynamic vars.
I can't understand why the "echo" doesn’t display any result for "$TAB['b']".
Do you know why ?
The test with error message : https://3v4l.org/Fp3GT
$TAB_a = "aaaaa";
$TAB['b'] = "bbbbb";
$TAB['b']['c'] = "ccccc";
$TAB_paths = ["_a", "['b']", "['b']['c']"];
foreach ($TAB_paths as $key => $value) {
echo "\n\n\${'TAB'.$value} : "; print_r(${'TAB'.$value});
}
You are treating the array access characters as if they are part of the variable name. They are not.
So if you have an array $TAB = array('b' => 'something');, the variable name is $TAB. When you do ${'TAB'.$value}, you're looking for a variable that's actually named $TAB['b'], which you don't have.
Since you say that you just want to be able to access array indexes dynamically based on the values in another array, you just put the indexes alone (without the array access characters) in the other array.
$TAB['b'] = 'bbbbbb';
$TAB['c'] = 'cccccc';
$TAB_paths = array('b', 'c');
foreach ($TAB_paths as $key => $value) {
echo "\n\n".'$TAB['."'$value'".'] : ' . $TAB[$value];
}
Output:
$TAB['b'] : bbbbbb
$TAB['c'] : cccccc
DEMO
It's unclear what you're trying to do, although you need to include $TAB_all in $TAB_paths:
$TAB_paths = [$TAB_all['a'], $TAB_all['aside']['nav']];
Result:
${TAB_all.aaaaa} : ${TAB_all.bbbbb} :
Not certain what you're needing. My guess you need to merge two arrays into one. Easiest solution is to use the array_merge function.
$TAB_paths = array_merge($TAB_a1, $TAB_a2);
You can define the variable first
foreach ($TAB_all as $key => $value) {
${"TAB_all" . $key} = $value;
}
Now Explore the result:
foreach ($TAB_all as $key => $value) {
print_r(${"TAB_all" . $key});
}

How to get value attirbute value from php 2d session variable

I am having an php session array like
["cart"]["123"] = "Biscuit"
["cart"]["124"] = "Jam"
If I want to access the 2nd element I will access array_values($_SESSION["cart"])[$i]
where $i runs in for loop. If I want to get the values "123" and "124", how can i achieve it in a for loop with only "cart" and $i..?
foreach($_SESSION['cart'] as $key => $value)
{
echo $key; // your 123 or 124 key
}
This is an associative array easiest and best way is foreach because have to deal with key-value pairs rather than indexes(numbers).
foreach($arr['cart'] as $key => $val){
echo "$key<br/>";
}
I used a variable to hold values($arr)
But you can try for loop also:
$keys = array_keys($arr['cart']);
for ($keyindex = 0; $keyindex < count($keys); $keyindex++) {
$key = $keys[$keyindex];
echo $key."<br>";
}

Multidimensional array - how to get specific values from array

I have converted Excel data into Php array using toArray()..
actually my result is
My question how to get value from this Multidimensional array? and also how to i get header and value from array in separately?
you can access value, by following the path of the array. so in your case :
$yourArrayName['Worksheet'][0][0], will return SNo
Get one value:
print_r($your_array['Worksheet'][0]);
Get values with loop:
foreach ($your_array as $key => $value)
{
echo $key; // 'Worksheet'
print_r $value;
}
If you are trying to extract all the values of specific key then you should use for or foreach or while loop...
its something like
<?php
$array = YOUR ARRAY;
$c=count($array);
for ( $i=0; $i < $c; $i++)
{
echo $array[$i]['StudentName'];
}
?>
else if you want to get a specific value manually You can easily traverse to your value as
$array = YOUR ARRAY
echo $ara['Worksheet'][0]['StudentName']

foreach() variable arrays within variable array

How can I retrieve all of the values from variable arrays within a variable array?
EDIT: I most likely don't know how to explain it well, but I want to know how I can display the values in $oranges and $melon within $variables. I thought it would be as simple as setting variables and placing them into an array but it returns "Array".
Example:
<?php
$oranges = array("0","1");
$melon = array("2","3");
$variables = array($oranges,$melon);
$i = 1;
foreach ($variables as $var) {
$name = "txt".$i;
echo "<input type='text' name='".$name."' value='".$var."' />";
$i++;
echo "<input type='file' name='image'>";
}
?>
Retrieving values of an array within another equals to retrieve a value from one single array. You just have to know what you want to do.
Suppose we have array $A and array $A1 and $A2 inside $A. foreach ($A as $innerArray) ... $innerArray the first inner array for the first loop, then the second etc ... If you want to get $innerArray values, make another loop, or get them directly if you already know the keys.
Exemple :
here are your vars:
$oranges = array("0","1");
$melon = array("2","3");
$variables = array($oranges,$melon);
Retrieve value of single array
echo $oranges[0]; // prints 0
echo $melon[1]; // prints 3
Retrieve values from array within another
foreach ($variables as $var) {
$name = "txt".$i;
echo "<input type='text' name='".$name."' value='".$var["INDEX"]."' />"; // INDEX must be an existing key in $oranges or $melon
$i++;
echo "<input type='file' name='image'>";
}
According to your code
$oranges = array("zero","one");
$melon = array("two","three");
$variables = array($oranges,$melon);
$i = 1;
foreach ($variables as $var) {
// $var[0] will contain "zero"
// $var[1] will contain "one"
//When $i increments to 2 on the next loop
// $var[0] will contain "two"
// $var[1] will contain "three"
}
If you want to access these values without a loop, it is as simple as
$variables[0][0] ---> "zero"
$variables[0][1] ---> "one"
$variables[1][0] ---> "two"
$variables[1][1] ---> "three"

getting information out of an array with key/value pairs

I have the following snippet of code that is creating the following array...
while ($stmt->fetch()) {
foreach($row as $key => $val) {
$x[$key] = $val;
}
$results[] = $x;
}
Results in the follow array:
Array ( [0] => Array ( [cap_login] => master [cap_pword] => B-a411dc195b1f04e638565e5479b1880956011badb73361ca ) )
Basically I want to extract the cap_login and cap_pword values for testing. For some reason I can't get it!
I've tried this kind of thing:
echo $results[$cap_login];
but I get the error
Undefined variable: cap_login
Can someone put me right here?
Thanks.
cap_login is in an array within $results so you would have to do $results[0]['cap_login']
You would have to do the following:
echo $x[0]['cap_login'] . '<br />';
echo $x[0]['cap_pword'];
The reson $results[$cap_login] wont work is because there isn't a variable called $cap_login, there is a string called cap login. In addition, there isn't a key in $results called $cap_login. There is a value in $results called 'cap_login'

Categories