i have an array:
$mainArr = ["SRI", "AIS", "GOW","SRI#AIS","SRI#GOW", "SRI#GOW#AIS"];
$strArr = ["SRI"];
i want to search the main Array with the given string Array element so that if the string is matched it should get the corresponding key,value pair.
expected o/p would be:
Array[
0->SRI
3->SRI#AIS
4->SRI#GOW
5->SRI#AIS#GOW
]
Any ideas ?
Thanks,
Srinivas
$mainArr = array("SRI", "AIS", "GOW","SRI#AIS","SRI#GOW", "SRI#GOW#AIS");
$strArr = array("SRI");
foreach ($mainArr as $key => $value)
{
foreach ($strArr as $str)
{
if (strpos($value,$str) !== false) $rez[$key] = $value;
}
}
var_dump($rez);
output:
array(4) {
[0]=> string(3) "SRI"
[3]=> string(7) "SRI#AIS"
[4]=> string(7) "SRI#GOW"
[5]=> string(11) "SRI#GOW#AIS"
}
I thing i ll help u,,
$test=array();
$mainArr = array("SRI", "AIS", "GOW","SRI#AIS","SRI#GOW", "SRI#GOW#AIS");
$strArr = array("SRI");
foreach ($mainArr as $key => $value)
{
$temp = explode('#',$value);
//$temp = $temp[0];
if(in_array($temp[0],$strArr))
$test[$key]=$value;
}
echo "<pre><span style='color:black; font-size:19;'>";print_r($test);echo "</span></pre>";
Use array_filter:
function filter($element)
{
return strpos($element, 'SRI') !== false;
}
$mainArr = array("SRI", "AIS", "GOW","SRI#AIS","SRI#GOW", "SRI#GOW#AIS");
$filteredArr = array_filter($mainArr, 'filter');
Related
I'm having an issue when I dump the array below. This dumps several arrays. Some are 2 some are 3, which complicates it even more. Basically what I want I put below. I have tried array_push, array_combine, array_merge, several different ways including $array[$param] = $insertValue and I'm stuck. I am open to creating a brand new array too.
Please note not all arrays are counts of 3 but always return at least 1.
Original array:
array(3) {
[0]=>
array(2) {
["contact_id"]=>
string(9) "CONTACTID"
["contact_id_content"]=>
string(19) "123456789123456"
}
[1]=>
array(2) {
["sm_owner"]=>
string(9) "SMOWNERID"
["sm_owner_content"]=>
string(19) "123456798452"
}
[2]=>
array(2) {
["contact_owner"]=>
string(13) "Contact Owner"
["contact_owner_content"]=>
string(16) "Jane Doe"
}
Array desired:
array(3) {
[0]=>
array(6) {
["contact_id"]=>
string(9) "CONTACTID"
["contact_id_content"]=>
string(19) "123456789123456"
["sm_owner"]=>
string(9) "SMOWNERID"
["sm_owner_content"]=>
string(19) "123456798452"
["contact_owner"]=>
string(13) "Contact Owner"
["contact_owner_content"]=>
string(16) "Jane Doe"
}
try this code:
$NewArray = array();
foreach($OriginalArray as $value) {
$NewArray[] = array_merge($value,$NewArray);
}
or you can use array_merge_recursive
let $result = [];
foreach ($yourarray as $key => $value) {
$result = $value;
}
var_dump($result);
Here you go: How to Flatten a Multidimensional Array? – Barmar 23 mins ago
function flatten($array)
{
return array_reduce($array, function($acc, $item){
return array_merge($acc, is_array($item) ? flatten($item) : [$item]);
}, []);
}
// loop the individual fields
for ($i=0; $i<count($row_data); $i++) {
$newArray = flatten([$i => $response_row]);
}
Try something like this:
function flatten(array $array) : array
{
$newArray = [];
foreach ($array as $subArray) {
foreach ($subArray as $key => $value) {
$newArray[$key] = $value;
}
}
return $newArray;
}
Hi let's say I have this array
array(2) {
[0]=>
string(9) "name|a-z+"
[1]=>
string(7) "id|0-9+"
}
Now I want a new array (or the same if possible) to be like this:
array(2) {
[name]=>
string(4) "a-z+"
[id]=>
string(4) "0-9+"
}
I think the solution implies explode and array_combine, but I am not good enough, can someone help me?
Thanks in advance.
function convert_my_array($arr){
$out = array();
foreach($arr as $obj){
$data = explode("|", $obj);
$out[$data[0]] = $data[1];
}
return $out;
}
Using the original array called $array here, loop through it set the values to what you want.
$newarray = array();
foreach ($array as $key=>$val) {
list($one, $two) = explode('|', $val);
$newarray[$one] = $two;
}
This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 6 months ago.
I have an array
array(5) {
[0]=>
array(1) {
["id"]=>
string(1) "5"
}
[1]=>
array(1) {
["id"]=>
string(1) "6"
}
[2]=>
array(1) {
["id"]=>
string(1) "7"
}
[3]=>
array(1) {
["id"]=>
string(1) "8"
}
[4]=>
array(1) {
["id"]=>
string(1) "9"
}
}
I wan to make my array like:
$registrationIDs = array( "5","6","7","8","9");
I am trying this code but not working
$results = array();
foreach($result as $inner) {
$results[key($inner)] = current($inner);
}
How do I effeciently transform arrays like this
Try with array_map.
$results = array_map (function ($e) { return $e['id']; }, $inner);
http://php.net/manual/en/function.array-map.php
By the way, if you still want to do it your way, try this form of foreach :
$results = array ();
foreach ($inner as $key => $value)
$results[$key] = $value['id'];
$array = [['id'=>1],['id'=>2],['id'=>3],['id'=>4],['id'=>5],];
$result = call_user_func_array('array_merge_recursive', $array);
var_dump($result['id']);
//array(5) {
// [0] =>
// int(1)
// [1] =>
// int(2)
// [2] =>
// int(3)
// [3] =>
// int(4)
// [4] =>
// int(5)
//}
Ok, I saw a lot of answers, so I wondered what to better answer was.
<?php
$data = array ();
for ($i = 0; $i < 1000000; $i++)
$data[$i] = array ('id' => rand ());
$time0 = microtime (true);
// Niols (1)
$results = array_map (function ($e) { return $e['id']; }, $data);
$time1 = microtime (true);
// Niols (2)
$results = array ();
foreach ($data as $key => $value)
$results[$key] = $value['id'];
$time2 = microtime (true);
// User (1)
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($data));
$results = iterator_to_array($it, false);
$time3 = microtime (true);
// User (2)
$results = array();
foreach ($data as $datum)
$results = array_merge($results, $datum);
$time4 = microtime (true);
// sectus
$results = call_user_func_array('array_merge_recursive', $data);
$time5 = microtime (true);
// Pankaj katiyar and Ghost
$results = array_column($data, 'id');
$time6 = microtime (true);
var_dump ($time1-$time0);
var_dump ($time2-$time1);
var_dump ($time3-$time2);
var_dump ($time4-$time3);
var_dump ($time5-$time4);
var_dump ($time6-$time5);
On my computer, this outputs :
float(0.62708687782288)
float(0.35285401344299)
float(1.5429890155792)
float(0.7408618927002)
float(0.70525908470154)
float(0.15015292167664)
Conclusion :
array_column is ultra-efficient (but PHP 5.5+). Writing a simple foreach seems quite efficient too.
You can try with below code.
I think this is working fine.
Process 1:-
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($data));
$l = iterator_to_array($it, false);
var_dump($l); // one Dimensional
Process 2:-
Try with:
$input = array(/* your array*/);
$output = array();
foreach ( $input as $data ) {
$output = array_merge($output, $data);
}
Try this:
$array = array(/*your array*/);
$results = array();
foreach ($array as $value)
{
foreach($value as $innerValue)
{
$results[] = $innervalue;
}
}
$images = valley_images();
var_dump($images);
$sorted_data = array();
foreach($images as $key => $value) {
if ($key == 'timestamp') {
$sorted_data[$value][] = $images;
}
}
ksort($sorted_data);
The error is appearing on this line:
$sorted_data[$value][] = $images;
When I do the var dump of images I receive this:
array(2) {
[0]=> array(2) {
["id"]=> string(2) "17" ["timestamp"]=> string(10) "1359797773"
}
[1]=> array(2) {
["id"]=> string(2) "20" ["timestamp"]=> string(10) "1359934365"
}
A nice way to do sorting of a key on a multi-dimensional array without having to know what keys you have in the array first:
<?php
$people = array(
array("name"=>"Bob","age"=>8,"colour"=>"red"),
array("name"=>"Greg","age"=>12,"colour"=>"blue"),
array("name"=>"Andy","age"=>5,"colour"=>"purple"));
var_dump($people);
$sortArray = array();
foreach($people as $person){
foreach($person as $key=>$value){
if(!isset($sortArray[$key])){
$sortArray[$key] = array();
}
$sortArray[$key][] = $value;
}
}
$orderby = "name"; //change this to whatever key you want from the array
array_multisort($sortArray[$orderby],SORT_DESC,$people);
var_dump($people);
I have an array of arrays that look like this:
array(40) {
[0]=>
array(2) {
["id"]=>
string(2) "ta"
["size"]=>
int(2)
[1]=>
array(2) {
["id"]=>
string(2) "tq"
["size"]=>
int(4)
....
I want to be able to get all the sizes in a way that I can do a query like this:
IN (2,4)
so... For each array, get the size key: IN (size,size,size...)
Thanks!
You could do something like this:-
$sizes = implode(',', array_map(function($v) { return $v['size']; }, $array));
Then just pass $sizes to your IN query
edit
In response to your comment below, you can use array_unique to remove duplicate sizes, eg:
$sizes = implode(',', array_unique(array_map(function($v) { return $v['size']; }, $array)));
Here you go:
$a = array("id"=>"ta","size"=>2);
$b = array("id"=>"tq","size"=>4);
$c = array($a,$b);
$in = array();
foreach ($c as $key=>$value) {
if(array_key_exists("size", $value)){
$in[] = $value["size"];
}
}
echo implode(",", $in);
$sizes = array();
foreach($array as $value) {
$sizes[] = $value['size'];
}
$query = implode(',', $sizes);
query ..." IN ($query) "..