0

I wonder if there's easy way to convert array which is in another array to string and keep it in that array ? The array which is inside array always consists of only 1 key. This is array that I have now:

array(6) {
  ["miestas"]=>
  string(2) "CC"
  ["checkbox"]=>
  array(1) {
    [0]=>
    string(1) "1"
  }
  ["kiekis"]=>
  string(5) "Three"
}

And this is the result what I want to get:

array(6) {
  ["miestas"]=>
  string(2) "CC"
  ["checkbox"]=>
  string(1) "1"
  ["kiekis"]=>
  string(5) "Three"
}

4 Answers 4

4

Read this: http://php.net/array

Use this: $array['checkbox'] = $array['checkbox'][0];

Sign up to request clarification or add additional context in comments.

Comments

2

You can type cast the value

$data['checkbox'] = (string) $data['checkbox'];

Comments

1

array_replace

$replacement = array('checkbox' => 1); 

$outputYouWant = array_replace($yourArray, $replacement);

print_r($outputYouWant);

Comments

1

Loops through the input array and checks if value is an array using is_array function. Pushes value array's value at index zero if an array otherwise pushes value to the result array.

$input = array('miestas' => 'CC', 'checkbox' => array("1"), 'kiekis' => 'Three');

$result = array();
foreach($input as $key=>$value) {
    $result[$key] = is_array($value) ? $value[0] : $value;  
}

// var_dump($result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.