Array stacking over another array - php

I'm trying to add an item to an array, but the arrays keep stacking over one another and it becomes unusable.
I have tried different methods and I can make it work if my input doesn't come from get_user_meta() and instead I create a custom string to test the array. But I need to load the meta so I can just add additional content into it.
$user_id = get_current_user_id();
$the_id = get_the_ID();
$continue_watching = get_user_meta($user_id,'continue_watching',false);
if ( !isset($continue_watching) ) {
$continue_watching = array();
}
$continue_watching[] = $the_id;
update_user_meta($user_id,'continue_watching',$continue_watching);
This is what is happening:
Array (
[0] => Array
(
[0] => Array
(
[0] => 16966
[1] => 16966
)
[1] => 11234
)
[1] => 16951
)
But I expected the output to look like this:
Array (
[0] => 11234
[1] => 16951
[2] => 16966
[3] => 16970
)
Update:
I have reset the array and this is the output just after $continue_watching = get_user_meta($user_id,'continue_watching',false);
Array
(
)
This is the output after $continue_watching[] = $the_id;
Array
(
[0] => 16955
)
After opening another page, this is the output after $continue_watching = get_user_meta($user_id,'continue_watching',false);
Array
(
[0] => Array
(
[0] => 16955
)
)
And this is the output after $continue_watching[] = $the_id;
Array
(
[0] => Array
(
[0] => 16955
)
[1] => 16957
)

After trying all the alternatives, it turned out to be a very simple solution. All I had to do was change the value from false to true inside the wordpress function get_user_meta($user_id,'continue_watching',false);
The "false" option was displaying the array inside another array, when I changed it to "true" the output was the array contained inside the variable alone.

Related

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);

Update a multidimentional array from key of other

I have a result array like this:
$result = Array
(
[1] => Array
(
[0] => some value
[1] => some value
[2] => some value
)
[4] => Array
(
[0] => some value
[1] => some value
[6] => Array
(
[0] => some value
[8] => Array
(
[0] => some value
)
)
)
)
and I have a second array
$test = Array
(
[4] => Array
(
[6] => Array
(
[8] => Array
(
[0] = value to add
)
)
)
)
How can I update the $result array on base of keys from $test array without losing any key index.
The main point is $result can have any structure and is dynamically generated but the $test array will always have keys index(s) which will somewhat match the $result array.
The general PHP array combine merge and other functions don't provide desired results.
[Edit]
The point is my second array $test will decide the position of $result where the data will be added/merged.
Did you try using:
http://php.net/manual/en/function.array-merge-recursive.php
By add do you mean append or addition(sum)??
This should work for appending the data.

php combine multidimensional arrays

I'm having trouble combining these two arrays so that the keys are kept together. The problem (I think) I'm having is that the arrays don't match in their structures, and the array keys are integers in one and names in the other. I feel like I need to have one array (feel free to correct me) so that I can display the prices coherently on the page, but I can't wrap my head around how to do it. I tried an array_merge, but it looses the indexed tlds sub-array:
$result = array();
foreach($cats[0]['domorder'] as $domorder) {
$result = array_merge($domorder, $prices[0]);
}
Maybe I can somehow (this isn't working either) add a 'price' sub-array that won't be overwritten?
$result = array();
$prc = array();
$prc['price'] = $prices[0];
foreach($prc as $p) {
$result = array_merge($p, $cats[0]['domorder'][0]);
}
Here's basically what I'm working with...my apologies if these are not formatted correctly for questions here.
Array 1, category definitions of hosting/domain name products:
Array
(
[0] => Array
(
[hosting] => Array
(
[0] => vpslinuxin
[1] => resellerhostinglinuxuk
[2] => resellerwindowshostinguk
........etc,etc.........
[34] => hosting
)
[domorder] => Array
(
[0] => Array
(
[dombiz] => Array
(
[0] => biz
)
)
[1] => Array
(
[dominfo] => Array
(
[0] => info
)
)
........etc,etc.........
Array 2, prices associated to the above categorized products:
Array
(
[0] => Array
(
[resellerhostinglinuxuk] => Array
(
[131] => Array
(
[renew] => Array
(
[1] => 43.19
)
[ssl] => 4.79
[add] => Array
(
[1] => 43.19
)
)
........etc,etc.........
[dombiz] => Array
(
[addtransferdomain] => Array
(
[1] => 10.69
)
[restoredomain] => Array
(
[1] => 69.95
)
[addnewdomain] => Array
(
[10] => 10.89
[9] => 10.89
)
........etc,etc.........
Anyone? I feel like this should be a fairly easy merge, but I can't figure out how to make it work.
Edit
Here's an example of how I think it should work:
Array
(
[0] => Array
(
[hosting] => Array
(
[vpslinuxin] => Array
(
[prices] => Array
(
[addons] => Array
(
.......
)
[plans] => Array
(
.......
)
)
)
)
[domorder] => Array
(
[0] => Array
(
[dombiz] => Array
(
[tlds] => Array
(
[0] => biz
)
[prices] => Array
(
[addtransferdomain] => Array
(
.......
)
[restoredomain] => Array
(
.......
)
[addnewdomain] => Array
(
.......
)
[renewdomain] => Array
(
.......
)
)
)
)
)
)
)
thanks for your help Michael but I managed to get it.
I was thinking too hard about it, so after dinner and some relaxing, I decided to simplify what I've been trying. There's no hard/fast rule saying that the two arrays need to be together - ultimately they're going to end up together anyway. So I just appended one to the other, defined by a 'product' and 'price' key:
$result = array();
$result[]['product'] = $cats[0];
$result[]['prices'] = $prices[0];

How can i get the values from this array?

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
)
)

Manually creating an associative array

I'm trying to create an associative array with dynamic data, and having some trouble.
I'd like to produce an array that looks like the following while fetching rows from a MySQL query.
Array
(
[0] = Array
(
[name] => First
)
[1] = Array
(
[name] => Second
)
[2] = Array
(
[name] => Third
)
[3] = Array
(
[name] => Fourth
)
[4] = Array
(
[name] => Fifth
)
)
I've been trying to use array_merge, but it's not giving me the result I want. Array_merge apparently doesn't operate the same inside a foreach as it does outside (I ran the same code with and without the loop, without worked the way I need).
Basically, this is what I'm doing currently (which doesn't work):
foreach($idList as $id)
{
$arr[] = array_merge(array(), array('name' => $id));
}
This gives me output like this:
Array
(
[0] = Array
(
[name] => first
)
[1] = Array
(
[0] = Array
(
[name] => first
)
[name] => second
)
[2] = Array
(
[0] = Array
(
[name] => first
)
[1] = Array
(
[0] = Array
(
[name] => first
)
[name] => second
)
[name] => third
)
)
You've got a few issues here.
Mainly, you can't have the same index twice. 'name' can be the index once and only once, so you're 'desired' output is impossible.
Also, this statement is pretty problematic
foreach($idList as $id)
{
$arr[] = array_merge(array(), array('name' => $id));
}
The use of $arr[] = $x is like a push. It adds a new element to the back of the array, numerically indexed.
Your use of array_merge is unnecessary. array_merge returns the second argument merged over the first argument. You are just trying to add a single new element. Also, is that exactly the line you used or did you use array_merge($arr, array('name' => $id)); ???
Try:
foreach($idList as $id)
{
$arr[] = array('name' => $id);
}
And you will get:
Array
(
[0] = Array
(
[name] => first
)
[1] = Array
(
[name] => second
}
....
And so on. I'm not sure if this is exactly what you want, but what you proposed in the first place isn't possible.

Categories