PHP populate single array with no keys - php

I have an indexed array like this:
$indexed = array(0=>2,1=>7,3=>9)
But i need a single array, without indexes, like this:
$notIndexed = array(2,7,9)
Zend_Form does not accept $indexed as parameter for the function populate() for multi checkboxes but works fine with $notIndexed
How can i dynamically transform $indexed to $notIndexed
Thanx for answers

$notIndexed = array_values($indexed);

Are you serious? Use, array_values($indexed).
http://php.net/manual/en/function.array-values.php

$notindex = array_values($indexed)

Related

How to format sql's return value in php?

In this case, I can get return value from sql.
$estateGroup =
Estate_base::select('id')->whereIn('name',$request->estateName)->get();
but $estateGroup will be like this: [{id:1},{id:2}]
I want to change [{id:1},{id:2}] to [1,2]
so I doing like following:
$estateGroup =
Estate_base::select('id')->whereIn('name',$request->estateName)->get();
$idGroup=[];
foreach ($estateGroup as $estate) {
array_push($idGroup, $estate->id);
}
Is there any way that I can format value more easier?
You can use array_map. By the way it will do same thing as you are doing but in less code.
$idGroup = array_map(function($estate) { return $estate->id;}, $estateGroup);
In PHP 7 you can use array_column() function with list of objects:
$idGroup = array_column($estateGroup, 'id');
It's shortest (and probably fastest) way to extract values of same property from multiple objects. The note from documentation:
7.0.0: Added the ability for the input parameter to be an array of objects.

Adding serialized data to an array - PHP

I have a three serialized data structures.
a:2:{i:0;s:151:"[["1","0","0","1","0","0","1","0","1","0","0","0"],["1","0","0","0","1","0","0","0","1","0","1","1"],["1","0","1","0","1","0","1","0","1","0","0","1"]]";i:1;s:151:"[["1","0","1","0","1","0","1","0","1","0","1","0"],["1","0","1","0","1","1","0","1","0","1","0","1"],["1","0","1","0","1","0","1","1","1","0","1","1"]]";}
a:2:{i:0;s:163:"[["10","0","0","0","30","0","0","60","0","0","0","0"],["20","0","0","30","0","0","20","0","0","0","50","0"],["30","0","0","0","20","0","0","30","0","20","0","30"]]";i:1;s:154:"[["20","0","0","0","0","0","0","0","0","0","0","0"],["30","0","0","0","0","0","0","0","0","0","0","0"],["40","0","0","0","0","0","0","0","0","0","0","0"]]";}
a:4:{i:0;s:151:"[["1","0","0","1","0","0","1","0","1","0","0","0"],["1","0","0","0","1","0","0","0","1","0","1","1"],["1","0","1","0","1","0","1","0","1","0","0","1"]]";i:1;s:151:"[["1","0","1","0","1","0","1","0","1","0","1","0"],["1","0","1","0","1","1","0","1","0","1","0","1"],["1","0","1","0","1","0","1","1","1","0","1","1"]]";i:2;s:151:"[["1","1","1","0","1","1","1","0","0","0","1","1"],["1","1","1","0","0","1","1","1","1","0","1","1"],["1","1","1","1","0","0","1","1","1","1","1","1"]]";i:3;s:151:"[["1","0","1","0","0","0","1","0","1","0","0","2"],["1","0","0","2","1","0","1","0","1","1","0","1"],["1","0","2","1","1","1","0","1","0","1","1","1"]]";}
I want to add all three serialized data into a single serialized array.
I tried this code and its work, but I want to be able to add additional data.
$data2=unserialize($value['monthly_forecast']);
$data1=unserialize($temp['monthly_forecast']);
//print_r($data1);
$combinedData = array($data1, $data2);
$monthly_forecast=serialize($combinedData);
$temp['monthly_forecast']=$monthly_forecast;
What about unserialize then array_merge() and then serialize the merged array back? Note that array_merge() can be used to
Merge one or more arrays
so you should be able to apply it in your case.
Edit:
You have a couple of options. You can either unserialize and merge everything into a single array and then serialize that. Or, you can also:
$array = array($serialized_data_1, $serialized_data_2, $serialized_data_3);
$all_serialized = serialize($array);
And then, to access the data:
$all_unserialized = unserialize($array);
$unserialized_data_1 = unserialize(all_unserialized[0]);
$unserialized_data_2 = unserialize(all_unserialized[1]);
$unserialized_data_3 = unserialize(all_unserialized[2]);

push an array in to multi dimensional array in php issue

I have multidimensional array like this:
$comp = [{"d":1},{"v":9}];
I need to insert another array
$arr = array("s" => 5 );
I tried the following code
$comp [] = array_push($comp,$arr);
I get the output $comp = [{"d":1},{"v":9},{"s":5},3]
I dont need 3.
So I have tried
$comp [] = json_encode(array_push(json_decode($comp),$arr));
But now it shows error.
Plz help me. I am using angular js with php.
You don't need array_push here. Just assign $arr to $comp[]
$comp[] = $arr;
Actually what you did here, let me explain it to you:
$comp[] - means pushback element.
It works the same way here as array_push($comp,$arr), but that function also returns the position where that push happened.
So you are pushing result of pushing the element as well as element itself :)
what you need is
$comp[] = $arr;
OR
array_push($comp,$arr);

pass arguments to a function php

Is it possible to convert an array to a list of elements without using list?
this works well
list($arg_a,$arg_b) = array($foo,$bar);
myfunction($arg_a,$arg_b);
but I'm looking for something similar to that:
$array = array($foo,$bar);
myfunction(php_builtin_function(array($foo,$bar)));
obviusly I can't edit that function!
function myfunction($param_a,$param_b){
...
}
As mentioned in comments, here's what you need:
call_user_func_array('myfunction', php_builtin_function(array($foo,bar)));
Docs
That said, it would be more readable to use list:
$result = php_builtin_function(array($foo,$bar));
list($arg_a, $arg_b) = $result;
myfunction($arg_a, $arg_b);

printing from multidimensional arrays?

I have a multi array that looks like this:
$_SESSION['cartItems']['quantity']
How do I print out its values? print_r won't work, unless $_SESSION doesn't support multi-dimensional arrays?
print_r($_SESSION['cartItems']); should work.
you can use var_dump($_SESSION). However, print_r should work. Make sure you are doing print_r($_SESSION), and not trying to print_r your variable that may not exist.
If you want to get the quantity of each card item in the array, you can do this:
$quantities = array_map(create_function('item', "$item['quanitity']"), $_SESSION['cardItems']);
// PHP 5.3
$quantities = array_map(function($item) {
return $item['quanitity'];
}, $_SESSION['cardItems']);

Categories