How can i get the values from this array? - php

In $attval when use foreach loop to print out the elements I get this output:
Array(
[0]
(
[id]=>1,
[name]=>xxx
)
[0]
(
[id]=>2,
[name]=>abc
)
)
For some reason both indices are the same.
I think I can still get the values using the multidimensional array, but i am confused as to how I can?

Assuming your code is something like this:
$attval = array();
$attval[0] = array("id"=>1,"name"=>"xxx");
$attval[1] = array("id"=>2,"name"=>"abc");
You can access individual properties like this:
$attval[0]['id']; // 1
$attval[1]['name']; // abc
You are showing a print_r of each sub-array, therefore your output should be:
Array
(
[id] => 1
[name] => xxx
)
Array
(
[id] => 2
[name] => abc
)
If you want a full view of the array you could just do:
print_r($attval);
Then you get:
Array
(
[0] => Array
(
[id] => 1
[name] => xxx
)
[1] => Array
(
[id] => 2
[name] => abc
)
)

Related

Array structure rearrangement without loop

I have an array like mentioned below, which I want to rearrange without using loop:
Array
(
[0] => Array
(
[Books] => Array
(
[id] => 4
)
)
[1] => Array
(
[Books] => Array
(
[id] => 3
)
)
[2] => Array
(
[Books] => Array
(
[id] => 2
)
)
[3] => Array
(
[Books] => Array
(
[id] => 1
)
)
)
I want an output like this:
Array(4,3,2,1)
I'm assuming you do not want to use for or foreach loops, but anything else that internally is or uses a loop is fine.
in this case, you can use array_map:
$result = array_map(function($item){
return $item['books']['id'];
}, $currentArray);
OR
if you do not even want that:
$v1 = array_column($input, 'books');
$result = array_column($v1, 'id');

Add common element to multi-dimensional array in php with out using loop

I have an array, which is given below:
$test = Array
(
[0] => Array
(
[0] => stud 1
)
[1] => Array
(
[0] => stud 2
)
[2] => Array
(
[0] => stud 3
)
);
I want to add a common element to above array with out using loop. For example, I want to add "test" to each element of array. After adding "test", array will look like:
$test = Array
(
[0] => Array
(
[0] => stud 1
[1] => 'test'
)
[1] => Array
(
[0] => stud 2
[1] => 'test'
)
[2] => Array
(
[0] => stud 3
[1] => 'test'
)
);
Is there any way to add common element array with out using any kind of loop(for, foreach etc...)?
You can use array_map(), check the live demo
array_map(function($v){$v[] = 'test'; return $v;}, $array);

I want replace index array. example $array[x][y] to $array[y][x]

Example. I have:
Array (
[0] => Array (
[comments_id] => 1
[comments_text] => blabla1
)
[1] => Array (
[comments_id] => 2
[comments_text] => blabla2
)
)
I want have:
Array (
[comments_id] => Array (
[0] => 1
[1] => 2
)
[comments_text] => Array (
[0] => blabla1
[1] => blabla2
)
In simplified wants to replace
$array[x][y] to $array[y][x]
I writing in php.
you can do it like this
// the final array which will hold your result array
// considering $results contains your previous array
$final_array = array();
foreach($results as $result) {
$final_array['comments_id'][] = $result['comments_id'];
$final_array['comments_text'][] = $result['comments_text'];
}

Multidimensional Array - Get unique values, but count all duplicates

I have a multi-dimensional array that I would like to get unique sub-values from, but also have a count of how many times those unique sub-values occurred.
For instance, this would be my starting array:
[0] => Array
(
[0] => Array
(
[id] => 1533438473619168
)
[1] => Array
(
[id] => 3333333333333333
)
)
[1] => Array
(
[0] => Array
(
[id] => 1533438473619168
)
[1] => Array
(
[id] => 5555555555555555
)
)
[2] => Array
(
[0] => Array
(
[id] => 1533438473619168
)
[1] => Array
(
[id] => 77777777777777777
)
)
In the end, I'd like to have an array that looks like this:
[0] => Array
(
[0] => Array
(
[id] => 1533438473619168
[count] => 3
)
[1] => Array
(
[id] => 3333333333333333
[count] => 1
)
[2] => Array
(
[id] => 5555555555555555
[count] => 1
)
[3] => Array
(
[id] => 77777777777777777
[count] => 1
)
)
Is there any general/easy way to do this without iterating through the first array for each value, comparing/storing the values in a temporary array, checking them, and adding to the count?
To get this exact format you may need to iterate thought your current array and do the counting manually, however php has the array_count_values() and array_unique() functions for this kind of thing:
http://php.net/manual/en/function.array-count-values.php
http://php.net/manual/en/function.array-unique.php
Because you are only concerned with the deepest values of the array, using array_walk_recursive seems suitable for this. Note that a reference to the output array $counted is used in the callback.
array_walk_recursive($ids, function($id, $k) use (&$counted) {
$counted[$id] = isset($counted[$id]) ? $counted[$id] + 1 : 1;
});
Using the id as the key in the $counted array will simplify the counting. The result of this will be somewhat different from your suggested output, but in my opinion it would actually be simpler to use. (e.g. foreach ($counted as $id => $count) {...).
$counted = array(
"1533438473619168" => 3
"3333333333333333" => 1
"5555555555555555" => 1
"77777777777777777" => 1);

What does [1] => 0 mean in this array?

I know this must be a fairly simple question, but I haven't managed to stumble across an answer yet.
I have the following array
$qid[0][0]=1;
$qid[1][0]=2;
$qid[2][0]=3;
$qid[3][0]=4;
When I use print_r($qid) I get the following
Array (
[0] => Array ( [0] => 1 [1] => 0 )
[1] => Array ( [0] => 2 )
[2] => Array ( [0] => 3 )
[3] => Array ( [0] => 4 )
)
I don't understand [1] => 0
in
[0] => Array ( [0] => 1 [1] => 0 )
If someone could explain what [1] => 0 means in this array, I'd greatly appreciate it. Thanks.
EDIT: It turns out that my array was indeed different to what I had written above, because it had been modified later in the code. Thanks everyone for the great answers. I'm still reading over them all and trying to make my mind understand them (Arrays turn my mind to jello).
[1] => 0 denotes an array element with the value 0.
The numbers in [] are array keys. So [1] is the second element of a numerically indexed array, (which starts with [0]), and the value of the second element ([1]) is 0.
PHP uses => as an operator to relate array keys/indices to their values.
So an overall explanation of this structure:
Array (
[0] => Array ( [0] => 1 [1] => 0 )
[1] => Array ( [0] => 2 )
[2] => Array ( [0] => 3 )
[3] => Array ( [0] => 4 )
)
The outer array is a numerically indexed array, and each of its elements is a sub-array. The first of them ([0]) is an array containing 2 elements, while the rest of them ([1] through [3]) are arrays containing only one single element.
That two-dimensional array is actually a one-dimensional array of arrays, which is why you're getting the nesting. The [x] => y bit simply means that index x of the array has the value y.
Now your output in this case doesn't actually match your code, since
$qid[0][0]=1;
$qid[1][0]=2;
$qid[2][0]=3;
$qid[3][0]=4;
print_r($qid);
produces:
Array (
[0] => Array ( [0] => 1 )
[1] => Array ( [0] => 2 )
[2] => Array ( [0] => 3 )
[3] => Array ( [0] => 4 )
)
If you wanted to get:
Array (
[0] => Array ( [0] => 1 [1] => 0 )
[1] => Array ( [0] => 2 )
[2] => Array ( [0] => 3 )
[3] => Array ( [0] => 4 )
)
(with the first array having two elements), you'd actually need:
$qid[0][0]=1;
$qid[0][1]=0;
$qid[1][0]=2;
$qid[2][0]=3;
$qid[3][0]=4;
print_r($qid);
You probably added a second item to $qid[0] somewhere ($qid[0][1] = 0). This code
$qid[0][0]=1;
$qid[1][0]=2;
$qid[2][0]=3;
$qid[3][0]=4;
outputs the the correct values for me (without [1] => 0:
Array ( [0] => Array ( [0] => 1 ) [1] => Array ( [0] => 2 ) [2] => Array ( [0] => 3 ) [3] => Array ( [0] => 4 ) )
It means that your index 0 in the original Array contains another Array of 2 items.
Specifically [1] => 0 means that the 2nd item of the "child" Array contains the number 0.
[1] => 0
in this simple way we can say that 1 is your array key and 0 is value for the 1 key
0 is store at the 1 key of the array
thanks
Simply put, you have a numerically indexed multidimensional array. http://php.net/manual/en/language.types.array.php should have all the information you need to read up on this.
As to why you have the [1] => 0, you'll need to look a little deeper into your code to see where it gets assigned.
I got the following result after printing out the array using print_r:
Array
(
[0] => Array
(
[0] => 1
)
[1] => Array
(
[0] => 2
)
[2] => Array
(
[0] => 3
)
[3] => Array
(
[0] => 4
)
)
I guess, you might have set a value for $gid[0][1] somewhere in your code.

Categories