how to match some elements in associative array in php - php

I have a following array
Array
(
[0] => Array
(
[0] =>
)
[1] => Array
(
[0] => flatrate
[1] => flatrate3
[2] => freeshipping
)
[2] => Array
(
[0] => flatrate
[1] => flatrate2
[2] => flatrate3
[3] => flatrate4
[4] => freeshipping
)
)
Now, i need to match some element in this like
if freeshipping in above array . it should echo yes else no.
Please suggest, how can i do this.

for($i = 0; count($array); $i++){
if(in_array($string, $array[$i]) echo true;
}

you can do simple code like that :
foreach ($array as $key) {
foreach ($key as $value) {
if($value == "your condition")
{
echo 'yes';
}else{
echo 'no';
}
}
}
try it.

Create A function
function checkfreeShipping($arrData)
{
foreach($arrData as $data){
if(in_array('freeshipping',$data))
return true;
}
}
Call defined function to check Shipping -
if(checkfreeShipping($cartArr)){
echo "free Shipping";
}
else{
echo "Paid Shipping";
}

Related

Set element inside array of array

If the 3rd element of array inside cars array is true i want to set others to be true .How to achieve it?
<?php
$cars = array
(
array(1,1,'f'),
array(2,2,'f'),
array(3,3,'t'),
array(4,4,'f')
);
foreach($cars as $keys){
if($keys[2]=='t')
$count=1;
}
foreach($cars as $keys){
if($count==1)
$keys[2] = 't';
}
print_r($cars);
?>
Just change 2 things as described below, Try:
$cars = array
(
array(1,1,'f'),
array(2,2,'f'),
array(3,3,'t'),
array(4,4,'f')
);
$count = 0; // declare $count
foreach($cars as $keys){
if($keys[2]=='t')
$count=1;
}
foreach($cars as $key=>$keys){
if($count==1)
$cars[$key][2] = 't'; // change value to t like this
}
output:
Array
(
[0] => Array
(
[0] => 1
[1] => 1
[2] => t
)
[1] => Array
(
[0] => 2
[1] => 2
[2] => t
)
[2] => Array
(
[0] => 3
[1] => 3
[2] => t
)
[3] => Array
(
[0] => 4
[1] => 4
[2] => t
)
)
You were almost close, Just make this change, use reference symbol & to actual change
from
foreach($cars as $keys){
to
foreach($cars as &$keys){
Check this : https://eval.in/609879
$exists= false;
foreach($cars as $car)
{
if($car[2] == 't')
{
$exists= true;
}
}
if($exists)
{
for($i=0; $i<count($cars); $i++)
{
$cars[$i][2] = 't';
}
}
Here's the solution for you
<?php
$cars = array(
array(1,1,'f'),
array(2,2,'f'),
array(3,3,'t'),
array(4,4,'f')
);
if(in_array('t', array_column($cars, 2))){
$cars = array_map(function($v){
$v[2] = 't';
return $v;
}, $cars);
}

Edit an array with new values in PHP

I have this array:
Array
(
[datas] => Array
(
[General] => Array
(
[0] => Array
(
[id] => logo
[size] => 10
)
)
[Rooms] => Array
(
[0] => Array
(
[id] => room_1
[size] => 8
)
[1] => Array
(
[id] => room_2
[size] => 8
)
[2] => Array
(
[id] => room_3
[size] => 8
)
)
)
)
I need to update it when I receive some info like this:
$key = 'room_3';
$toChange = '9';
So in my array, I want to change the size of room_3.
I will always edit the same element (i.e. size).
What I tried:
// Function to communicate with the array
getDatas($array, 'room_3', '9');
function getDatas($datas, $got, $to_find) {
foreach ($datas as $d) {
if (array_search($got, $d)) {
if (in_array($to_find, array_keys($d))) {
return trim($d[$to_find]);
}
}
}
}
But it does not work...
Could you please help me ?
Thanks.
function getDatas($datas, $got, $to_find) {
foreach($datas['datas'] as $key => $rows) {
foreach($rows as $number => $row) {
if($row['id'] == $got) {
// u can return new value
return $row['size'];
// or you can change it and return update array
$datas['dates'][$key][$number]['size'] = $to_find; // it should be sth like $new value
return $datas;
}
}
}
}
function changeRoomSize (&$datas, $roomID, $newSize ){
//assuming that you have provided valid data in $datas array
foreach($datas['datas']['Rooms'] as &$room){
if($room['id'] == $roomID){
$room['size'] = $newSize;
break;//you can add break to stop looping after the room size is changed
}
}
}
//--- > define here your array with data
//and then call this function
changeRoomSize($data,"room_3",9);
//print the results
var_dump($data);
It's a 3-dimensional array, if you want to change the value, do like this:
$key = 'room_3';
$toChange = '9';
$array['datas'] = getRooms($array['datas'], $key, $toChange);
function getRooms($rooms, $key, $toChange) {
foreach($rooms as $k1=>$v1) foreach ($v1 as $k2=>$v2) {
if ($v2['id'] == $key)) {
$rooms[$k1][$k2]['size'] = $toChange;
}
}
return $rooms;
}
print_r($array);

Get the value from array and check value is present or not

I have an array look like this
$langs = Array
(
[source] => Array
(
[0] => Array
(
[0] => Arabic
)
[1] => Array
(
[0] => Azerbaijani
)
)
[target] => Array
(
[0] => Azerbaijani
[1] => Array
(
[0] => Amharic
[1] => Burmese
)
[2] => Array
(
[0] => English
[1] => German
)
)
)
Now I want to search a value from target key. So I have my code look like this
$target = array();
array_push($target, 'English'); // want to search from the above array so I made it push to array
foreach( $langs['target'] as $langs ) {
if(in_array( $target, $langs )) {
echo 'Got the value';
}
else {
echo 'not got values';
}
}
But its not working. So can someone kindly tell me how to get the values? Any help and suggestions wil be really appreciable. Thanks
You have to do this recursively, iterating through each of the arrays, as in_array does not search recursively.
Try this:
$target = array(
'Azerbaijani',
array('Amharic','Burmese'),
array('English','German')
);
function search_recursive($find, $where) {
foreach ($where as $element) {
if (is_array($element)) {
if (search_recursive($find, $element))
return true;
}
elseif ($find == $element) {
return true;
}
}
return false;
}
if (search_recursive('English', $target)) echo "FOUND";
else echo "Not Found";
This has not been thoroughly tested, so make sure you test properly.
try this:
$got=false;
foreach( $langs['target'] as $lan=> $langs) {
foreach( $langs as $lang=> $lan){
if($target[0]==$lan ) {
$got=true;
break;
}else{
continue;
}
}
}
if($got){
echo "got it";
}else{
echo "not found";
}
NOTE: in_array does not work for multidimensional array.

How to display values inside multi-dimensional array in php

I would like to get the values of id and name inside this array.
Array
(
[data] => Array
(
[0] => Array
(
[id] => 238345159549706
[members] => Array
(
[data] => Array
(
[0] => Array
(
[id] => 100001130889528
[name] => Sy Cheeze
)
[1] => Array
(
[id] => 100002616426665
[name] => Chun Jenny
)
.......
I've tried using this foreach.
foreach ($acquaintances as $acquaintance)
{
foreach ($acquaintance as $acquaint)
{
$acqID = $acquaint['id'];
$acqName = $acquaint['name'];
echo $acqName;
}
}
but nothing will be displayed. What would I do with my code? Any idea and suggestions please. Thank you!
$array = array
(
array("bla",22,18),
array("blaa",15,13),
array("blaaa",5,2),
array("blaaaa",17,15)
);
for ($row = 0; $row < 4; $row++) {
echo "<p><b>Row number $row</b></p>";
echo "<ul>";
for ($col = 0; $col < 3; $col++) {
echo "<li>".$array[$row][$col]."</li>";
}
echo "</ul>";
}
You can also access the indices directly in your foreach loop. Like this:
foreach($acquaintances['data'] as $acquaintance) {
foreach($acquaintance['members']['data'] as $acquaint) {
$acqID = $acquaint['id'];
$acqName = $acquaint['name'];
echo $acqName . '<br/>';
}
}

Array Looping and Counting String Occurrences PHP

Anyone know what I'm doing wrong?
From the following array I want to count the the number of times "Friday" occurs.
Array (
[404979509517702] => Array (
[0] => 235
[1] => 04:10,Friday
)
[404045862944400] => Array (
[0] => 192
[1] => 23:52,Wednesday
)
[20403274909688162] => Array (
[0] => 186
[1] => 22:21,Tuesday
)
[202735273075459] => Array (
[0] => 336
[1] => 04:29,Tuesday
)
[652948031457462] => Array (
[0] => 410
[1] => 06:22,Monday
)
[2606749954978] => Array (
[0] => 312
[1] => 05:01,Saturday
)
[755318061725] => Array (
[0] => 384
[1] => 04:51,Friday
)
)
This is what I'm doing:
$friday = array();
foreach ($the_array as $friday){
$the_array = explode(',', $friday[1]);
$the_array[$the_array [1]] += ($friday[1]);
}
print_r($friday);
This way, I get the wrong number of "Friday" occurrences.
Any idea what I'm doing wrong or if there is a more elegant solution to that?
I would try something like the following if all you want to do is count occurrences:
$friday_count = 0;
foreach($the_array as $record) {
// Search for "Friday"
if(stristr($record[1],'Friday') !== false) {
$friday_count++; // increment count
}
}
$fridayCount = 0;
foreach ($the_array as $friday){
$friArr = explode(',', $friday[1]);
if($friArr[1] == "Friday") {
$fridayCount++;
}
}
echo ($fridayCount);
The code should be:
$fridayCount = 0;
foreach ($the_array as $friday){
if(strrpos($friday[1], 'Friday') !== false) {
$fridayCount++;
}
}
echo $fridayCount;
I think you might be complicating this unnecessarily. You should be able to test for 'Friday' and increment a counter ($friday, here) as follows:
$friday = 0;
foreach ($the_array as $element){
if (array_pop(explode(',', $element[1])) == 'Friday') {
$friday++;
}
}
echo $friday;
$cnt = 0;
foreach ($the_array as $friday){
if (explode(',', $friday[1]) == 'Friday') {
$cnt++;
}
}
echo $cnt;

Categories