PHP Multidimensional Array return error - php

I have an array called $reads when I do an var_dump($reads), I get the below array result.
I am trying to get first item of first array with var_dump($reads[0][0]). I get a Message: Error rendering view: [home.uploaded] Undefined offset: 0
array(161) {
[0]=>
array(4) {
["517a5745e8505"]=>
string(29) "Ngee Ann Poly_Keywords report"
["517a5745e86fe"]=>
string(0) ""
["517a5745e882e"]=>
string(0) ""
["517a5745e89b5"]=>
string(0) ""
}
[1]=>
array(4) {
["517a5745e8505"]=>
string(7) "Keyword"
["517a5745e86fe"]=>
string(6) "Clicks"
["517a5745e882e"]=>
string(11) "Impressions"
["517a5745e89b5"]=>
string(3) "CTR"
}
[2]=>
array(4) {
["517a5745e8505"]=>
string(18) "accounting diploma"
["517a5745e86fe"]=>
string(1) "2"
["517a5745e882e"]=>
string(3) "364"
["517a5745e89b5"]=>
string(5) "0.55%"
}
[3]=>
array(4) {
["517a5745e8505"]=>
string(11) "polytechnic"
["517a5745e86fe"]=>
string(4) "1940"
["517a5745e882e"]=>
string(5) "42995"
["517a5745e89b5"]=>
string(5) "4.51%"
}
[4]=>
array(4) {
["517a5745e8505"]=>
string(15) "tourism diploma"
["517a5745e86fe"]=>
string(1) "1"
["517a5745e882e"]=>
string(3) "156"
["517a5745e89b5"]=>
string(5) "0.64%"
}

Try this
var_dump($reads[0]["517a5745e8505"]);
for what you want as per comments do this, put you array in a $arr variable and follow what I am doing.
$firstelementvalues = array();
$i = 0;
foreach ($arr as $key=>$val) {
$x = 0;
foreach ($val as $value) {
if ($x == 0) {
$firstelementvalues[] = $value;
$x = 1;
}
}
$i++;
}
print_r($firstelementvalues);
Output is
Array
(
[0] => Ngee Ann Poly_Keywords report
[1] => Keyword
[2] => accounting diploma
)

Because there is no value in array with 0 offset so please try like
var_dump($reads[161][0]);

I think that you need to use loop (for,foreach) if you would like to display AND use the data.
var_dump is: Arrays and objects are explored recursively with values indented to show structure.

It's because your array doesn't have element [0][0].
If you want to select first element in second dimension array you can use current:
$lev1 = current($yourArray);
$lev2 = current($lev1);

Your reads array has no numeric keys in the second dimension. You could do something like this, if you have no clue about the keys:
$read = $reads[0];
// I am getting all keys now, because I guess you also want to process the rest of that data
$readKeys = array_keys($read);
var_dump($read[ $readKeys[0] ] );

Related

Putting Array with same values into another array?

I have one big array which has a load of similar values but I want to put all the arrays with the same value into another array. For example, I have this array.
array(4) {
[0]=>
array(8) {
["symbol"]=>
string(3) "aaaa"
["name"]=>
string(7) "aaaa"
["buy_price"]=>
string(14) "100.0000000000"
["current_worth"]=>
string(14) "100.2500000000"
}
[3]=>
array(8) {
["symbol"]=>
string(3) "aaa"
["name"]=>
string(7) "aaaaa"
["buy_price"]=>
string(14) "100.0000000000"
["current_worth"]=>
string(14) "100.2500000000"
}
[2]=>
array(8) {
["symbol"]=>
string(3) "xxx"
["name"]=>
string(7) "xxxxx"
["buy_price"]=>
string(14) "100.0000000000"
["current_worth"]=>
string(14) "100.2500000000"
}
}
I want to be able run this array through a foreach loop and then output the array results together that all have the same name. Like too
Name aaa
-- Name aaa [0]
-- Name aaa [1]
Name xxx
-- Name xxx [0]
I am struggling how to do the logic.
If I understand correctly, you need some reducing. Assuming that $origin_array contain what you need to transform:
$result = array_reduce($origin_array, function ($carry, $item) {
$name = $item['name'];
$carry[$name][] = $item;
return $carry;
}, []);
This code will make 2-dimensional array where elements grouped by name field of origin array.
Explanation
The best explanation will be to write analogical foreach loop:
$my_reduce = function ($carry, $item) { // callback, 2-nd param
$name = $item['name'];
$carry[$name][] = $item;
return $carry;
};
$result = []; // initial value, 3 param
foreach($origin_array as $item) {
$result = $my_reduce($result, $item);
}
This is roughly speaking what happens under the hood of array_reduce function.

how to compare multiple arrays for higher value based on one specific key?

I have multiple arrays. I want to compare these arrays key values with one another and output only one array which has the higher value than others. Here is my array
array(4) { ["type"]=> string(6) "Person" ["relevance"]=> string(8) "0.645481" ["count"]=> string(1) "1" ["text"]=> string(15) "RAJESH NELATURI" }
array(4) { ["type"]=> string(6) "Person" ["relevance"]=> string(8) "0.567918" ["count"]=> string(1) "2" ["text"]=> string(11) "Layoutlands" }
array(4) { ["type"]=> string(6) "Person" ["relevance"]=> string(8) "0.546824" ["count"]=> string(1) "1" ["text"]=> string(9) "N. Rajesh" }
I want to compare the key "relevance" and filter the array which has higher relevance value. In this case The first array which has the value "0.645481" and print the arrays key[text]. Here it should print RAJESH NELATURI
Here is a solution :
$higher = array('relevance' => 0);
foreach($myArray as $key) {
if($key['relevance'] > $higher['relevance']){
$higher = $key;
}
}
At the end, $higher will have the biggest relevance and is equal to :
$higher = array(4) { ["type"]=> string(6) "Person" ["relevance"]=> string(8) "0.645481" ["count"]=> string(1) "1" ["text"]=> string(15) "RAJESH NELATURI" }
now you can print the higher text :
echo $higher['text'];
UPDATE:
But it seems all 'relevance' values in your array are string, So they can not be compared with each other! you have to change them to float,
Try to produce this array in another way to have float values,
If you can't, Try this code instead :
$higher = array('relevance' => 0);
foreach($myArray as $key) {
if((float)$key['relevance'] > (float)$higher['relevance']){
$higher = $key;
}
}
this code changes string to float before compare.
find the relevance with max value, and the index. Here is the code, hope it helps.
$relevances = array_column($array, 'relevance');
$values = array_values($relevances);
$map = array_combine($values, array_keys($$relevances));
echo $array[$map[max($values)]]['text'];
Iterate through the arrays in a replace-if-higher basis.
$currentMax = PHP_INT_MIN; // it is impossible for any entries to be smaller than this value
foreach($arrays as $array){
$keyValue = $array["relevance"]; // not exactly sure what you mean by "key value"
if($keyValue > $currentMax){
$currentMax = $keyValue;
$currentName = $array["text"]'
}
}
$currentName should be set as lonig as $arrays is not empty and the $keyValue of any entries is greater than PHP_INT_MIN.

How to convert array to string in codeigniter?

I am new to PHP.
I want to convert this array;
array(2) {
[0]=> object(stdClass)#24 (1) {
["item"]=> string(1) "2"
}
[1]=> object(stdClass)#25 (1) {
["item"]=> string(1) "1"
}
}
to string like this
string(1) "2", string(2)"1", string(3)"0"...
What is the way to do this?
Note: i try add to "row()" in php code. but always single result.
for example: only string(1)"2"
You are asking something weird. But here is an answer:
let say $arr is your
array(2) { [0]=> object(stdClass)#24 (1) { ["item"]=> string(1) "2" }
[1]=> object(stdClass)#25 (1) { ["item"]=> string(1) "1" } }
then your code to convert it into that string you mentioned will be:
$str=''; $idx=0;
foreach ($arr as $obj) {
$str.=($idx==0?'':', ').'string ('.$idx++.') "'.$obj->item.'"';
}
Now you have your weird string in $str var.
Or if my first weird guess is wrong. Here is another that has more sense to me, but not asked by you:
foreach ($arr as $obj) {
echo $obj->item; // or do whatever you want with this value which is string type
}
Looks you have an array object, you need to use a foreach loop here, and your array values are "2", "1". Not string(1) "2" , string(1) "1", You use a var_dump so the arrays shows with the type and length.
array(2) {
[0]=> object(stdClass)#24 (1) {
["item"]=> string(1) "2"
}
[1]=> object(stdClass)#25 (1) {
["item"]=> string(1) "1"
}
}
To get each value using foreach, see the below example, let your array name is $arr.
foreach($arr as $val){
echo $val->item;
}
This will display the array values as: "2", "1".

How to make array first value as key for the second value as value in php array

I have arrays structured like below:
array(2) {
["uid"]=>
string(2) "39"
["name"]=>
string(18) "Manoj Kumar Sharma"
}
array(2) {
["uid"]=>
string(2) "47"
["name"]=>
string(11) "S kK Mishra"
}
I want these array should be like this below:
array(4) {
[39]=>
string(18) "Manoj Kumar Sharma"
[47]=>
string(11) "S kK Mishra"
}
How can i achieve this ? Please help me.
Updated
You can try this with array_column() -
$new = array_column($arr, 'name', 'uid');
Demo
Note: array_column() not available for PHP < 5.5
If you are using lower versions of PHP the use a loop.
$new = array();
foreach($your_array as $array) {
$new[$array['uid']] = $array['name'];
}

PHP - Compare 2 multidimensional arrays and output values if equal fields in array

I have 2 Arrays in 2 variables and both of them containing exactly the same values in the fields (in the 1st array = "image_id"-field and in the 2nd array = "ID-field").
I need to compare the 2 fields and would like to output the imagepath string of the 1st array (if the "ID"-field of 1st array and the field of 2nd array are equal)
Something like this:
if "2146" from 1st multi-array is equal to "2146" from 2nd multi-array, then echo apple.jpg..
But how does that work? Its really freakin me out the last days.. thanks in advance for your replies.
$multidimensional_array1:
array(4) {
[0]=>
string(9) "apple.jpg"
["imagepath"]=>
string(9) "apple.jpg"
[1]=>
string(4) "2146"
["image_id"]=>
string(4) "2146"
}
array(4) {
[0]=>
string(10) "ananas.jpg"
["imagepath"]=>
string(10) "ananas.jpg"
[1]=>
string(4) "2037"
["image_id"]=>
string(4) "2037"
}
array(4) {
[0]=>
string(8) "nuts.jpg"
["imagepath"]=>
string(8) "nuts.jpg"
[1]=>
string(4) "2024"
["image_id"]=>
string(4) "2024"
}
$multidimensional_array2:
array(2) {
[0]=>
string(4) "2146"
["ID"]=>
string(4) "2146"
}
array(2) {
[0]=>
string(4) "2037"
["ID"]=>
string(4) "2037"
}
array(2) {
[0]=>
string(4) "2024"
["ID"]=>
string(4) "2024"
}
As long as the arrays have the same keys, length and order, you can iterate over one and and pick values from both.
$len = count($arr1);
for ($i = 0; $i < $len; $i++)
{
if ($arr1[$i]['image_id'] == $arr2[$i]['ID'])
{
// output $arr1[$i]['imagepath']
}
}
If the information is from two tables in the same database, you would be better off by just joining the tables together. If the arrays are not ordered the same or not of the same length (so that $i might reference different elements in both arrays), use one as a lookup table:
$lookup = array();
foreach ($arr2 as $element)
{
$lookup[$element['ID']] = $element;
}
foreach ($arr1 as $element)
{
if (isset($lookup[$element['image_id']]))
{
// output $element['imagepath']
}
}
foreach($multidimensional_array1 as $arr1){
foreach($multidimensional_array2 as $arr2){
if($arr2['id']==$arr1['image_id']){
echo $arr1['imagepath'];
}
}
}
Note: The larger the arrays become the longer this will take.

Categories