I'd like to extract only up to a certain part of the name and test if it's set (isset)?
Specifically for largeImage_1, largeImage_2, etc..
This is what I've tried, but it doesn't seem to return or test.
if ($_POST['tutorial']) {
foreach ( $_POST['tutorial'] as $key => $value ) {
if ('largeImage' === substr($key, 0 , 10) )
{
echo $value;
}
}
}
Array
(
[title] => title
[tutorial] => Array
(
[1] => Array
(
[largeImage_1] => Array
(
[0] => image.png
[1] => image.png
)
)
[2] => Array
(
[largeImage_2] => Array
(
[0] => image.png
)
)
)
[name] => "";
)
You need a loop within another loop to access keys like 'largeImage_1', 'largeImage_2'.
if ($_POST['tutorial']) {
foreach ( $_POST['tutorial'] as $key => $value ) {
foreach ( $value as $k => $v ) {
if ('largeImage' === substr($k, 0 , 10) )
{
//your code here
}
}
}
}
Related
I'm trying to change my array from this:
Array
(
[0] => Array
(
[BID_OPEN] => Array
(
[0] => 0.718282
)
)
[1] => Array
(
[BID_CLOSE] => Array
(
[0] => 1.654545
)
)
[2] => Array
(
[BID_OPEN] => Array
(
[0] => 1.654878
)
)
)
in to this:
Array
(
[BID_OPEN]
(
[0] => 0.718282
[1] => 1.654878
)
[BID_CLOSE]
(
[0] => 1.654545
[1] => 1.645845
)
)
I'm not sure how to begin. My code:
foreach($array as $keys=>$values)
{
if(!empty($array [$c]['BID_OPEN']))
{
$inital_part1 = array("BID_OPEN", $array [$c]['BID_OPEN']);
}
else
{
echo '';
}
if(!empty($array [$c]['BID_CLOSE']))
{
$inital_part2 = array("BID_CLOSE", $array [$c]['BID_CLOSE']);
}
else
{
echo '';
}
$array1[] = $inital_part1;
$array1[] = $inital_part2;
$c++;
}
I seem to get double outputs, so the foreach when I build arrays is giving me two times the required output. Google reckons it's because I have an array in my array somewhere but I'm precisely sure I don't.
The array came from an object stdclass and I don't know what that is, have googled but haven't found anything useful. Also I'm able to get some figures but only the initial values are correct, the rest of the data doesn't seem to come through. No doubt it's because I used an index[0] to get it working.
After hours any help would be great thanks.
As long as you have told us everything about your input array it can be done quite simply like this
<?php
$in = [ ['BID_OPEN' => [0.718282]],
['BID_CLOSE' => [1.654545]],
['BID_OPEN' => [1.654878]]
];
print_r($in);
$new = []; // new array we are building
foreach ($in as $abid) {
if (array_key_exists('BID_OPEN', $abid) ) {
$new['BID_OPEN'][] = $abid['BID_OPEN'][0];
}
if (array_key_exists('BID_CLOSE', $abid) ) {
$new['BID_CLOSE'][] = $abid['BID_CLOSE'][0];
}
}
print_r($new);
THE INPUT ARRAY: Is like yours
Array
(
[0] => Array
(
[BID_OPEN] => Array
(
[0] => 0.718282
)
)
[1] => Array
(
[BID_CLOSE] => Array
(
[0] => 1.654545
)
)
[2] => Array
(
[BID_OPEN] => Array
(
[0] => 1.654878
)
)
)
RESULT:
Array
(
[BID_OPEN] => Array
(
[0] => 0.718282
[1] => 1.654878
)
[BID_CLOSE] => Array
(
[0] => 1.654545
)
)
$c = 0;
$array1['BID_OPEN'] = [];
$array2['BID_CLOSE'] = [];
foreach($vartttttt as $tunips=>$ert)
{
$d = 0;
foreach($ert as $erts=>$val)
{
//$array[] = $erts;
if($erts == 'BID_OPEN')
{
array_push($array1['BID_OPEN'], $val[0]);
}
if($erts == 'BID_CLOSE')
{
array_push($array2['BID_CLOSE'], $val[0]);
}
$d++;
}
$c++;
}
$array = array_merge($array1, $array2);
I have the following array i want to show the count of inner array values Please help me out.
Array
(
[e1549b20-4cad-11e6-85b4-73d5cb14d4fe] => Array
(
[a029e160-4337-11e6-8db4-ad7de57838b4] => Array
(
[0] => b46b70a2-481a-11e6-8b19-00262d644487
[1] => b4696a1e-481a-11e6-8b19-00262d644487
)
[40eca780-48ef-11e6-8a04-eb9fe0a25fc5] => Array
(
[0] => b46b70a2-481a-11e6-8b19-00262d644487
[1] => b4696a1e-481a-11e6-8b19-00262d644487
)
[e5926390-44cf-11e6-bc85-19a184fbd10f] => Array
(
[0] => b4696a1e-481a-11e6-8b19-00262d644487
)
[51a44c00-4a53-11e6-81fe-313fe319f95b] => Array
(
[0] => b4696a1e-481a-11e6-8b19-00262d644487
)
)
)
Please Try with this functionality :
function getCount($arr, $count = 0) {
foreach ($arr as $value) {
if (is_array($value)) {
$count = getCount($value, $count);
} else {
$count = $count + 1;
}
}
return $count;
}
echo getCount($arr);
I want to group an array by a subarray's value. If I have an array like this:
Array
(
[0] => Array
(
[userID] => 591407753
[propertyA] => 'text1'
[propertyB] => 205
)
[1] => Array
(
[userID] => 989201004
[propertyA] =>'text2'
[propertyB] => 1407
)
[2] => Array
(
[userID] => 989201004
[propertyA] => 'text3'
[propertyB] => 1407
)
)
I want to sort to group this array by a subarray's value so I can have an array like this:
Array
(
[0]=>Array
(
[userID]=>59140775
[properties]=>Array
(
[0]=>text1
)
[propertyB]=>205
)
[1]=>Array
(
[userID]=>989201004
[properties]=>Array
(
[0]=>'text2'
[1]=>'text3'
)
[propertyB]=>1047
)
)
How can I make this?
Before I had tried this:
$result = array();
foreach ($userArray as $record)
{
$id=$record['userID'];
if(isset($result[$id]))
{
$result[$id]['propertyA'][]=array($record['propertyA']);
}
else {
$record["propertyA"]=array($record['propertyA']);
unset($record['tweet']);
$result[$id]=$record;
}
}
the problem was for the propertyA. I was an the result an additional property propertyA with the table like this:
Array
(
[0]=>Array (
[userID]=>989201004
[propertyA]=>'text2'
[properties]=>Array(
[0]=>'text2'
[1]=>'text3'
)
)
)
The following code should do the job. I hope it is self-explanatory:
$result = array();
foreach ($array as $record) {
if (!isset($result[$record['userID']])) {
$result[$record['userID']] = array(
'userID' => $record['userID'],
'properties' => array($record['propertyA']),
'propertyB' => $record['propertyB'],
);
}
else {
$result[$record['userID']]['properties'][] = $record['propertyA'];
}
}
$result = array_values($result);
I have two arrays:
array one is named as $input that output:
Array
(
[0] => Array
(
[reference_no] => 306334510
[archive_no] => 20140102900152506
)
[1] => Array
(
[reference_no] => 22936619
[archive_no] => 20140104900153643
)
[2] => Array
(
[reference_no] => 30211132
[archive_no] => 20140109001536461
)
[3] => Array
(
[reference_no] => 3890623027301
[archive_no] => 201401029001949791
)
)
and second array named as $active that will output:
Array
(
[0] => Array
(
[archive_no] => 20140102900152506
)
[1] => Array
(
[archive_no] => 20140104900153643
)
[2] => Array
(
[archive_no] => 2014010900133107
)
[3] => Array
(
[archive_no] => 2014010900152506
)
[4] => Array
(
[archive_no] => 2014010900153643
)
)
So first I need to check for duplicated rows and I check it by 'archive_no'.
So far this works fine, but now I need to remove those duplicated rows from $input array.
My unset does not seem to work.
code so far:
public function RemoveDublicatedRows($input, $active){
$output = array();
foreach ($input as $inp) {
foreach ($active as $act) {
if ($act['archive_no'] == $inp['archive_no']) {
$output[] = $act;
foreach ($output as $out){
if($inp['archive_no'] == $out['archive_no']){
$inp = array($inp);
unset($input[$inp]);
}
}
}
}
}
return $output;
}
You need to have an index instead of an array ($inp) here:
unset($input[$inp]);
Should be:
if($inp['archive_no'] == $out['archive_no'])
{
$index = array_search($inp, $input);
unset($input[$index]);
}
EDIT: some parts are useless in your code, here is a cleaned code:
public function RemoveDublicatedRows($input, $active)
{
$output = array();
foreach ($input as $inp)
{
foreach ($active as $act)
{
if ($inp['archive_no'] == $act['archive_no'])
{
$output[] = $act;
// maybe you should consider storing $act['archive_no'] instead ?
// It's up to you, depends on what you need...
$index = array_search($inp, $input);
unset($input[$index]);
}
}
}
return $output;
}
For example I have code like this, I'm doing foreach loop and it works fine
foreach ( $response['albums']['album'] as $key=>$album ) {
echo($album['artist']['name']);
}
The content of $response is
[albums] => Array
(
[album] => Array
(
[0] => Array
(
[name] => Recovery
[playcount] => 1176206
)
[1] => Array
(
[name] => The Eminem Show
[playcount] => 948632
)
)
)
But, when the source of $response consist only from one element, i have such code, and foreach loop will not work.
[albums] => Array
(
[album] => Array
(
[name] => Doo-Wops & Hooligans
[playcount] => 1106732
)
)
So the question is how to let it work with minimal changes. Please excuse me for my beginner English.
Try like this..
if(array_key_exists(0,$response['albums']['album']))
{
foreach ( $response['albums']['album'] as $key=>$album ) {
echo($album['artist']['name']);
}
}
else
{
foreach ( $response['albums']['album'] as $album ) {
echo $album['name'];
}
}
foreach ( $response['albums'] as $album ) {
if(isset(album['name']))
{
echo $album['artist']['name'];
}
else
{
foreach($album as $val)
{
echo $val['artist']['name'];
{
}
}
That will work, although ideally your array should look like:
[albums] => Array
(
[0] => Array
(
[name] => Recovery
[playcount] => 1176206
)
[1] => Array
(
[name] => The Eminem Show
[playcount] => 948632
)
)
That way even if there is still one you can foreach it.
it will be better to keep consistent structure of input array $response to use single loop otherwise you can try following.
if(count($response['albums']) == 1){
foreach($response['albums'] as $key =>$val) {
echo $val['album']['name'];
}
}
else {
foreach ( $response['albums']['album'] as $key=>$album ) {
echo($album['artist']['name']);
}
}